我添加了 JtextField 行,但在输出中仅显示按钮。我错过了什么,请用 java 代码写下你的答案以便清楚。谢谢!
public JPanel createContentPane (){
JButton Button1
buttonPanel = new JPanel();
buttonPanel.setLayout(null);
buttonPanel.setLocation(10, 50);
buttonPanel.setSize(1370, 770);
totalGUI.add(buttonPanel);
Button1 = new JButton("Button 1");
Button1.setLocation(0, 0);
Button1.setSize(120, 30);
Button1.addActionListener(this);
buttonPanel.add(Button1);
JLabel l;
l= new JLabel;
JTextField a = new JTextField();
a.setVisible(true);
a.setLocation(1000,200);
l.add(a);
}
这是主要方法和createAndShowGUI
private void createAndShowGUI() {
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("[=] JButton Scores! [=]");
//Create and set up the content pane.
ButtonExample_Extended demo = new ButtonExample_Extended();
frame.setContentPane(demo.createContentPane());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1370, 770);
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new ButtonExample_Extended().createAndShowGUI();
}
});
}
最佳答案
我认为您正在寻找类似的东西:
public JPanel createContentPane() {
buttonPanel = new JPanel();
buttonPanel.setLayout(null);
buttonPanel.setLocation(10, 50);
buttonPanel.setSize(1370, 770);
JButton Button1 = new JButton("Button 1");
Button1.setLocation(0, 0);
Button1.setSize(120, 30);
buttonPanel.add(Button1);
JTextField a = new JTextField(20);
a.setLocation(125, 0);
a.setSize(120, 30);
buttonPanel.add(a);
return buttonPanel;
}
1) 尝试使用LayoutManager而不是 buttonPanel.setLayout(null);
、setLocation()
、setSize()
。
2) a.setVisible(true);
这里不是必须的,默认是true
。
3) 您无需将 JTextField
添加到 JLabel
。将组件添加到容器,例如 JPanel
。
4)我推荐您阅读swing tutorial about components
5) 发布 SSCCE将来。
关于java - 如何将 JTextField 添加到具有 JButton 的 Jpanel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20042365/