我不知道为什么我的JFrame
没有显示,而且它真的让我烦恼,我觉得我错过了一些简单的事情,而我却没有看到它。所以任何帮助都会很棒!
这是代码:
import java.awt.*;
import java.awt.EventQueue;
import javax.swing.*;
import javax.swing.GroupLayout.*;
public class Lorenzo_ChatClient_class extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Lorenzo_ChatClient_class frame = new Lorenzo_ChatClient_class();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/** Create the frame. */
public Lorenzo_ChatClient_class() {
//setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
JFrame frame = new JFrame();
frame.setSize(100,100);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
textField_1 = new JTextField();
textField_1.setBounds(0, 244, 450, 34);
contentPane.add(textField_1);
textField_1.setColumns(10);
JButton btnNewButton = new JButton("Send");
btnNewButton.setBounds(351, 6, 99, 122);
contentPane.add(btnNewButton);
JButton btnNewButton_1 = new JButton("Quit");
btnNewButton_1.setBounds(351, 124, 99, 122);
contentPane.add(btnNewButton_1);
JTextArea textArea_1 = new JTextArea();
textArea_1.setBorder(UIManager.getBorder("EditorPane.border"));
textArea_1.setBounds(6, 6, 345, 240);
contentPane.add(textArea_1);
JTextArea textArea = new JTextArea();
textField = new JTextField();
textField.setColumns(10);
}
}
答案 0 :(得分:0)
显然,我过快地阅读了你的代码,因为你确实有Lorenzo_ChatClient_class扩展JFrame。这大大改变了解决方案。
我认为你的主要方法看起来很好。你不应该在构造函数中调用可覆盖的方法;它导致了很多错误。尝试将代码更改为:
import java.awt.*;
import java.awt.EventQueue;
import javax.swing.*;
import javax.swing.GroupLayout.*;
import javax.swing.border.*;
class Lorenzo_ChatClient_class extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Lorenzo_ChatClient_class frame = new Lorenzo_ChatClient_class();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Lorenzo_ChatClient_class() {
super();
frameSetup();
setVisible(true); // or just keep it in your main method
}
private void frameSetup() {
this.setBounds(100, 100, 450, 300);
this.setSize(100, 100);
this.contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
textField_1 = new JTextField();
textField_1.setBounds(0, 244, 450, 34);
contentPane.add(textField_1);
textField_1.setColumns(10);
JButton btnNewButton = new JButton("Send");
btnNewButton.setBounds(351, 6, 99, 122);
contentPane.add(btnNewButton);
JButton btnNewButton_1 = new JButton("Quit");
btnNewButton_1.setBounds(351, 124, 99, 122);
contentPane.add(btnNewButton_1);
JTextArea textArea_1 = new JTextArea();
textArea_1.setBorder(UIManager.getBorder("EditorPane.border"));
textArea_1.setBounds(6, 6, 345, 240);
contentPane.add(textArea_1);
JTextArea textArea = new JTextArea();
textField = new JTextField();
textField.setColumns(10);
}
}
我只是尝试过,这对我有用,虽然它是一个小框架。不确定setbounds(100, 100, 450, 300)
之后是setSize(100,100)
紧随其后的原因。您将大小设置为450x300,然后立即将大小重置为100x100。我删除了setSize(100, 100)
行,JFrame打开了很多。
答案 1 :(得分:0)
在您的主要方法中,
Lorenzo_ChatClient_class frame = new Lorenzo_ChatClient_class();
在你的构造函数中,
setBounds(100, 100, 450, 300);
JFrame frame = new JFrame();
frame.setSize(100,100);
You have taken class reference and JFrame names are same "frame".