【发布时间】:2013-11-23 10:48:05
【问题描述】:
所以当我遇到这个问题时,我正在开发这个小程序。它主要是关于使用 GridBagLayout 设置的位置,它不会向我显示 JLable 的文本。另一个问题是,我的进度是工作停止工作,后来又回来了。知道它是什么吗?我也想要有人帮我解决位置问题我想把 JTextField 放在左下角有什么帮助吗?我无法解决这个问题,也没有信息在线专门帮助我。所以也许你可以帮助我。她的代码到目前为止......
package Main;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class code extends JFrame{
public static JTextField consol;
public static String title = "Metal-Lock:The Start";
public static Dimension size = new Dimension(650, 550);
public static JPanel panel;
public static JButton enter;
public static JLabel output;
public static void main(String args[]) {
code frame = new code();
}
public code() {
setTitle(title);
setSize(size);
setResizable(true);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
// VISITOR LIST
CONSOL();
P1();
P2();
}
//******************************************************************************************************************************
public void CONSOL() {
consol = new JTextField(30);
consol.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
String input = consol.getText();
output.setText(input);
}});
final JButton enter = new JButton("Enter");
enter.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
String input = consol.getText();
output.setText(input);
panel.add(consol);
add(panel);
panel.add(enter);
add(panel);
output = new JLabel();
panel.add(output);
add(panel);
}
});
}
//******************************************************************************************************************************
public void P1() {
}
//******************************************************************************************************************************
public static JLabel grid1;
public static JLabel grid2;
public static JLabel grid3;
public static JLabel grid4;
public static JLabel grid5;
public void P2() {
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints R = new GridBagConstraints();
JLabel grid1 = new JLabel ("Hello"); panel.add(grid1, R);
R.gridx = 0; R.gridy = 0;
JLabel grid2 = new JLabel ("Hello"); panel.add(grid2, R);
R.gridx = 0; R.gridy = 0;
JLabel grid3 = new JLabel ("Hello"); panel.add(grid3, R);
R.gridx = 0; R.gridy = 0;
JLabel grid4 = new JLabel ("Hello"); panel.add(grid4, R);
R.gridx = 0; R.gridy = 0;
JLabel grid5 = new JLabel ("Hello"); panel.add(grid5, R);
R.gridx = 0; R.gridy = 0;
}
}
【问题讨论】:
-
在容器中的哪里添加组件??
-
如果您使用的是 Eclipse,您可以使用 UI 编辑器。对于实例 WindowBuilder。之后你也可以从生成的代码中学习。
-
请您 Gábor Csikós 向我展示窗口构建器,但现在我必须使输入按钮工作,以便它显示我输入的内容。她喜欢我的新问题! stackoverflow.com/questions/20169052/…
-
无关:请学习java命名约定并遵守它们