package com.igswing.zmz;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
class MyJDialog1 extends JDialog {
public MyJDialog1(PrimeTable frame1) {
super(frame1, "质数判断", true);
Panel myPanel = new Panel();
myPanel.add(new JLabel("是质数"));
setLocationRelativeTo(null);
setSize(150, 150);
}
}
class MyJDialog2 extends JDialog {
public MyJDialog2(PrimeTable frame1) {
super(frame1, "质数判断", true);
Panel myPanel = new Panel();
myPanel.add(new JLabel("不是质数"));
setLocationRelativeTo(null);
setSize(150, 150);
this.add(myPanel);
}
}
public class PrimeTable extends JFrame{
public PrimeTable() {
super("判断质数");
JLabel j1 = new JLabel("输入一个整数");
// j1.setBounds(100, 200, 200, 50);
JPanel p1 = new JPanel();
Container c = getContentPane();
p1.add(j1);
JTextField j11 = new JTextField(20);
p1.add(j11);
c.add(p1);
JButton b1 = new JButton("判断");
b1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String ss = j11.getText();
int a = Integer.parseInt(ss);
int i;
for(i = 2; i <= Math.sqrt(a*1.0); i++) {
if(a%i==0)
break;
}
if(i==Math.sqrt(a*1.0)+1) {
new MyJDialog1(PrimeTable.this).setVisible(true);
}
else {
new MyJDialog2(PrimeTable.this).setVisible(true);
}
}
});
p1.add(b1);
setLayout(new GridLayout());
setLocationRelativeTo(null);
setSize(400, 200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new PrimeTable();
}
}
如图我想在新的JDialog显示"12不是质数"
accountField = new JTextField(20);
passwordField = new JPasswordField(20);
获得账号和密码
String getaccount = accountField.getText();
String getpassword = new String(passwordField.getPassword());
以上是我获得用户输入的账号和密码的写法,你参考一下,也是这种写法,调用变量名(红色字体的)就好了
不是啊,我的意思是从父框哪里获得数据,不过现在解决了,直接传参数就行了,傻逼了