运行程序时,JTextField和JTextArea随机不起作用

use*_*637 1 java swing jtextfield jtextarea

我现在只是学习java而且我想弄清楚出了什么问题,但每次运行代码我都会得到不同的结果.这是一个程序,我或多或少地想弄清楚如何使用java,但有些对象并没有按照应有的方式出现.

package main;

import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.*;
//import java.awt.event.*;

public class Main extends JFrame {

 JButton button = new JButton();
 JTextField textField = new JTextField();
 JTextArea textArea = new JTextArea();
 int buttonClicked;

public Main() {
    Toolkit tk = Toolkit.getDefaultToolkit(); // creates toolkit
    Dimension screenSize = tk.getScreenSize(); // sets screen dimensions
    Dimension frameDim = new Dimension(400, 400); // sets frame dimensions
    int xPos = (screenSize.width / 2) - (frameDim.width / 2); // sets xPos
    int yPos = (screenSize.height / 2) - (frameDim.height / 2); // sets yPos

    this.setSize(frameDim); // sets jframe size
    this.setVisible(true); // sets jframe visible
    this.setLocation(xPos, yPos); // sets jframe location to xPos and yPos
    this.setResizable(false); // sets Resizable to false
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // exits on close
    this.setTitle("This is a Frame");

    JPanel panel = new JPanel(); // creates a panel
    this.add(panel); //adds panel to JFrame

    JLabel label = new JLabel("I'm a label"); // creates a label with text
    label.setText("I say something"); // changes the text in the label
    label.setToolTipText("this is a label"); // sets the tool-tip
    panel.add(label); // adds the label to the panel

    JButton button = new JButton("I am a button"); // creates a button
    button.setText("I am still a button"); // changes text on the button
    button.setBorderPainted(true); // adds border (default)
    button.setContentAreaFilled(true); // adds area inside border (default)
    button.setToolTipText("It's a button"); // sets the tool-tip
    panel.add(button); // adds the button to the panel

    JTextField textField = new JTextField("words", 15); // creates textField
    textField.setColumns(10); // sets textField size
    textField.setText(""); //changes textField text
    textField.setToolTipText("this is a textField"); // sets the tool-tip
    panel.add(textField); //adds textField to the panel

    JTextArea textArea = new JTextArea(15, 20);
    textArea.setLineWrap(true);
    textArea.setWrapStyleWord(true);
    panel.add(textArea);
    JScrollPane scrollBar = new JScrollPane(textArea,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    panel.add(scrollBar);
    this.add(panel);


}

public static void main(String[] args) {
    new Main();
//      new TestTextArea();
}
}
Run Code Online (Sandbox Code Playgroud)

问题是,当我运行时,无论textArea和textField是否出现在窗口中,这都是一个黑暗的镜头.有时,但很少,按钮和标签甚至没有出现...我想不出解决方案.

如果这有帮助,我正在运行java 7 update 60,它也无法在更新55上运行

JB *_*zet 7

每个Swing教程都会对此进行解释,但我会再次重复.setVisible(true)在添加所有元素之前,不应该调用.这应该是你做的最后一件事,就在打电话之后this.pack().

此外,Swing组件不应该在事件disptach线程之外使用.你正在主线程中做所有事情.阅读http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html

最后,面板应该添加一次,而不是两次.


And*_*son 5

将呼叫转移setVisible(true)到代码的末尾.

其他提示:

  1. 查看@trashgod的建议.
  2. 不要设置框架的大小,而是pack()在添加所有组件后调用.
  3. 要在屏幕中央设置GUI,请使用setLocationRelativeTo(null)但更好的方法是setLocationByPlatform(true)在GUI可见之前使用.对于框架定位,你不能去setLocationByPlatform(true).有关演示,请参阅此答案.