好的,所以我正在尝试定位我的 JButton,但如果我将“this.setLayout”设置为 null,我的按钮不会显示,但如果我放置一个布局,按钮就会出现。我真的想定位我的按钮而不是使用布局..我尝试过使用容器,面板(见下文),只是经常..没有任何效果:l
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class BingoHelper extends JFrame implements WindowListener, ActionListener{
JTextField text = new JTextField(20);
JPanel pnlButton = new JPanel();
private JButton b; {
b = new JButton("Click to enter name");
}
public void actionPerformed (ActionEvent e) {
String fn = JOptionPane.showInputDialog("Username:");
String sn = JOptionPane.showInputDialog("Password:");
JOptionPane.showMessageDialog(null, "Welcome " + fn + " " + sn + ".", "", JOptionPane.INFORMATION_MESSAGE);
text.setText(fn + " " + sn);
b.setVisible(false);
text.setVisible(true);
}
public BingoHelper(){
super("BINGO");
this.setLayout(new GridBagLayout());
add(text);
text.setVisible(false);
this.add(pnlButton);
pnlButton.add(b);
pnlButton.setVisible(true);
pnlButton.setLocation(800,800);
b.setVisible(true);
b.setPreferredSize(new Dimension(150,40));
b.addActionListener(this);
}
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
public void windowOpened(WindowEvent e) {}
public void windowActivated(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowDeactivated(WindowEvent e) {}
public void windowClosed(WindowEvent e) {}
}