Window 在 Java 中使用 JButton 打开后显示不正确
Window not appearing properly after opening with JButton in Java
我的 java 应用程序有一个非常奇怪的问题。我基本上是单击 JButton 和新的 window I want to open 打开但没有内容显示。事情是这样的。
如果我 运行 单独使用 class 而不使用 JButton,它 运行 确实如此。
这是按钮的代码。
public Create()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 629, 316);
contentPane = new JPanel();
contentPane.setBackground(new Color(255, 255, 204));
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
btnBuildData = new JButton("Build Graph");
btnBuildData.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
//CreateTest frame = new CreateTest();
new CreateTest().setVisible(true);
}
});
btnBuildData.setBackground(Color.WHITE);
btnBuildData.setForeground(Color.BLACK);
btnBuildData.setBounds(29, 59, 107, 23);
contentPane.add(btnBuildData);
这对我来说很奇怪,因为我已经将此代码用于其他 classes 并且它按预期工作。我尝试了很多不同的方法来做同样的事情,但其中 none 行得通。这是我用按钮打开的框架的一些代码。
public class CreateTest extends JFrame {
public CreateTest() {
}
//Create the connection to Neo4j
Driver driver = GraphDatabase.driver( "bolt://localhost", AuthTokens.basic( "*****", "*******" ) );
Session session = driver.session();
StatementResult resultVariable;
Record recordVariable;
//Create variables to manage communication with Neo4j
String resultString = new String();
Query neoQuery = new Query();
private JTextField progressTextField;
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
new CreateTest().initUI();
}
});
}
protected void initUI()
{
final JFrame frame = new JFrame();
//the form
frame.setTitle(CreateTest.class.getSimpleName());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button = new JButton("Click here to add data");
button.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
doWork();
}
});
progressTextField = new JTextField(25);
progressTextField.setEditable(false);
frame.getContentPane().add(progressTextField, BorderLayout.NORTH);
frame.getContentPane().add(button, BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
}
protected void doWork() {
SwingWorker<Void, Integer> worker = new SwingWorker<Void, Integer>() {
@Override
protected Void doInBackground() throws Exception {
CreateTest frame = new CreateTest();
}
@Override
protected void process(List<Integer> chunks) {
progressTextField.setText(chunks.get(chunks.size() - 1).toString());
}
@Override
protected void done() {
progressTextField.setText("Success: All Nodes have been added ");
}
};
worker.execute();
}
}
两者有区别windows。一个是绝对布局,另一个是 jpanel,但我认为这不是问题所在。如果有人有任何想法,请告诉我,我们将不胜感激。谢谢。
您使用 new CreateTest() 调用空构造函数
public CreateTest() {
}
它什么都不做,因为你的代码在它之外。
我的 java 应用程序有一个非常奇怪的问题。我基本上是单击 JButton 和新的 window I want to open 打开但没有内容显示。事情是这样的。
如果我 运行 单独使用 class 而不使用 JButton,它 运行 确实如此。
这是按钮的代码。
public Create()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 629, 316);
contentPane = new JPanel();
contentPane.setBackground(new Color(255, 255, 204));
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
btnBuildData = new JButton("Build Graph");
btnBuildData.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
//CreateTest frame = new CreateTest();
new CreateTest().setVisible(true);
}
});
btnBuildData.setBackground(Color.WHITE);
btnBuildData.setForeground(Color.BLACK);
btnBuildData.setBounds(29, 59, 107, 23);
contentPane.add(btnBuildData);
这对我来说很奇怪,因为我已经将此代码用于其他 classes 并且它按预期工作。我尝试了很多不同的方法来做同样的事情,但其中 none 行得通。这是我用按钮打开的框架的一些代码。
public class CreateTest extends JFrame {
public CreateTest() {
}
//Create the connection to Neo4j
Driver driver = GraphDatabase.driver( "bolt://localhost", AuthTokens.basic( "*****", "*******" ) );
Session session = driver.session();
StatementResult resultVariable;
Record recordVariable;
//Create variables to manage communication with Neo4j
String resultString = new String();
Query neoQuery = new Query();
private JTextField progressTextField;
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
new CreateTest().initUI();
}
});
}
protected void initUI()
{
final JFrame frame = new JFrame();
//the form
frame.setTitle(CreateTest.class.getSimpleName());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button = new JButton("Click here to add data");
button.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
doWork();
}
});
progressTextField = new JTextField(25);
progressTextField.setEditable(false);
frame.getContentPane().add(progressTextField, BorderLayout.NORTH);
frame.getContentPane().add(button, BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
}
protected void doWork() {
SwingWorker<Void, Integer> worker = new SwingWorker<Void, Integer>() {
@Override
protected Void doInBackground() throws Exception {
CreateTest frame = new CreateTest();
}
@Override
protected void process(List<Integer> chunks) {
progressTextField.setText(chunks.get(chunks.size() - 1).toString());
}
@Override
protected void done() {
progressTextField.setText("Success: All Nodes have been added ");
}
};
worker.execute();
}
}
两者有区别windows。一个是绝对布局,另一个是 jpanel,但我认为这不是问题所在。如果有人有任何想法,请告诉我,我们将不胜感激。谢谢。
您使用 new CreateTest() 调用空构造函数
public CreateTest() {
}
它什么都不做,因为你的代码在它之外。