【发布时间】:2013-10-17 05:41:41
【问题描述】:
好的,所以我正在尝试定位我的 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) {}
}
【问题讨论】:
-
if I set the "this.setLayout" to null...- 不要使用空布局!!! Swing 旨在与layout managers 一起使用。因此,与不同的布局管理器一起玩,看看它们是如何工作的,记住你也可以嵌套布局管理器。 -
我真的想定位我的按钮,而不是使用布局。不,你不需要。也许你还没有意识到,但你真的没有
标签: java swing layout jpanel jbutton