JAVA-Can';t将文本字段和按钮添加到容器
我正在尝试做一个小程序,使用一些按钮和文本字段。 我能够用JAVA-Can';t将文本字段和按钮添加到容器,java,swing,user-interface,Java,Swing,User Interface,我正在尝试做一个小程序,使用一些按钮和文本字段。 我能够用JPanel创建窗口,但不知道如何添加按钮和文本字段 我使用的代码是: public UI() { sprites = new HashMap(); // spriteCache = stage.getSpriteCache(); JFrame okno = new JFrame ("VoLTE Script"); setBounds(0,0,SZEROKOSC,WYSOKOSC); JPanel
JPanel
创建窗口,但不知道如何添加按钮和文本字段
我使用的代码是:
public UI() {
sprites = new HashMap();
// spriteCache = stage.getSpriteCache();
JFrame okno = new JFrame ("VoLTE Script");
setBounds(0,0,SZEROKOSC,WYSOKOSC);
JPanel panel = (JPanel)okno.getContentPane();
panel.setLayout (null);
panel.add(this);
okno.setBounds(0,0,800,600);
okno.setVisible(true);
JTextField pole = new JTextField(10);
JButton przycisk = new JButton("teasda");
przycisk.setPreferredSize(new Dimension(300, 350));
przycisk.setLayout(new BorderLayout());
panel.add(przycisk);
przycisk.setVisible(true);
pole.setBounds (300,300,200,200);
pole.setLayout(null);
pole.setVisible(true);
panel.add(pole);
okno.addWindowListener(new WindowAdapter(){
public void windowClosing (WindowEvent e){
System.exit(0);
}
});
okno.setResizable(false);
createBufferStrategy(2);
strategia=getBufferStrategy();
requestFocus();
// addKeyListener(this);
// addMouseListener(this);
}
您需要正确使用布局,当使用边框布局时,您需要告诉它要使用哪个边框(、BorderLayout.NORTH或其他内容),请查看oracles页面上的教程
另外,想想你如何命名你的字段等。命名为“przycisk”只是给我一个不进一步阅读代码的理由。此代码来自此网站: 对于未来,我建议您使用扩展JFrame,因此您只能编写这样的gui,而无需初始化JFrame:
public class CalculatorGUI extends JFrame {
public CalculatorGUI() {
setTitle("Calculator");
setBounds(300, 300, 220, 200);
}}
我建议您查看一些关于如何创建Java Gui或其他内容的教程。谢谢您的帮助,对于波兰语名称(已修复)表示抱歉。 我能够添加滚动文本区域。 面板中似乎存在问题。添加(此);放在按钮和文本字段之前。 根据我的理解,如果panel.add(此)设置在panel.add(极)之前;然后在前面设置panel.add(这个),并添加了杆,但看不到 下面是我的实际工作代码: 。。。 公共用户界面(){ })); okno.setresizeable(false)
P.S.是不应该的。有些用户对自己的母语更为熟悉。请尊重这一点。非常感谢。
public class CalculatorGUI extends JFrame {
public CalculatorGUI() {
setTitle("Calculator");
setBounds(300, 300, 220, 200);
}}
sprites = new HashMap();
// spriteCache = stage.getSpriteCache();
JFrame okno = new JFrame ("VoLTE Script");
setBounds(0,0,WIDTH,HEIGHT);
JPanel panel = (JPanel)okno.getContentPane();
panel.setLayout (null);
okno.setBounds(0,0,800,600);
okno.setVisible(true);
JTextArea pole = new JTextArea();
pole.setLayout(null);
pole.setLineWrap(true);
//pole.setBackground(Color.BLACK);
pole.setEditable(false);
JScrollPane scroll = new JScrollPane(pole);
scroll.setBounds(25,250,500,300);
scroll.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
panel.add(scroll);
panel.add(this);
okno.addWindowListener(new WindowAdapter(){
public void windowClosing (WindowEvent e){
System.exit(0);
}
createBufferStrategy(2);
strategia=getBufferStrategy();
requestFocus();
// addKeyListener(this);
addMouseListener(this);
}
...