Java 需要初始化JFrame
我不知道如何初始化JFrame窗口。我需要写什么到init im 我已在主要阶段创建了以下内容:Java 需要初始化JFrame,java,swing,Java,Swing,我不知道如何初始化JFrame窗口。我需要写什么到init im 我已在主要阶段创建了以下内容: Panel Panel=new Panel(); Panel.init(); JFrame frame = new JFrame("Shape Project"); frame.add(Panel); frame.setResizable(false); frame.setSize(new Dimension(1200, 650)); frame.setDefaultCloseOperation
Panel Panel=new Panel();
Panel.init();
JFrame frame = new JFrame("Shape Project");
frame.add(Panel);
frame.setResizable(false);
frame.setSize(new Dimension(1200, 650));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
在JPanel课程中,我写了以下内容:
public class Panel extends JFrame
{
public void init()
{
}
}
但当我激活帧时,它不会激活。我需要在窗口将打开的init func中写些什么?试试pack()代码>JFrame的方法。如果您计划使用Swing进行开发,我建议您遵循本教程:
试试pack()代码>JFrame的方法。如果您计划使用Swing进行开发,我建议您遵循本教程:
您已经有了JFrame(frame)。因此,现在应该为面板添加组件(也可以在主类中添加)。这些组件包括JTextField、JButton等(甚至还有另一个JPanel),每个组件都可以使用panel.add(组件名称)添加到面板中
还建议按照前面提到的教程进行操作。您已经有了JFrame(frame)。因此,现在应该为面板添加组件(也可以在主类中添加)。这些组件包括JTextField、JButton等(甚至还有另一个JPanel),每个组件都可以使用panel.add(组件名称)添加到面板中
还建议遵循前面提到的教程Erkan。您的面板类应该扩展JPanel
,而不是JFrame。
您可以向JPanel
添加组件,例如JButton、JList等
这是一个示例代码,如果不创建自己的类,则需要初始化JFrame:
public class LogMain
{
public static void main(String[] args)
{
JFrame window = new JFrame("Log");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize(300,300);
window.setResizable(false);
JPanel panel = new JPanel();
JButton openFile = new JButton("Btn1");
JButton openDir = new JButton("Btn2");
panel.add(openFile);
panel.add(openDir);
window.add(panel);
window.setLocationRelativeTo(null);
window.setVisible(true);
}
}
您的面板
类应该扩展JPanel
,而不是JFrame。
您可以向JPanel
添加组件,例如JButton、JList等
这是一个示例代码,如果不创建自己的类,则需要初始化JFrame:
public class LogMain
{
public static void main(String[] args)
{
JFrame window = new JFrame("Log");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize(300,300);
window.setResizable(false);
JPanel panel = new JPanel();
JButton openFile = new JButton("Btn1");
JButton openDir = new JButton("Btn2");
panel.add(openFile);
panel.add(openDir);
window.add(panel);
window.setLocationRelativeTo(null);
window.setVisible(true);
}
}
这是我关于JFrame的init的示例:
public class Windows{
public static void main(String args[]){
SJFrame window = new SJFrame("NEWNEWNEW");
window.init();
}
}
public class SJFrame extends JFrame(){
public SJFrame(String s){
super(s);
}
void init(){
Container panel = this.getContentPane();
panel.setBackground(Color.green);
panel.setLayout(new GridLayout(5,1));
JLabel jl1 = new JLabel("UserName");
JLabel jl2 = new JLabel("PassWord");
this.add(jl1);
this.add(jl2);
this.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE) ;
this.setSize(300,100);
this.pack();
this.setVisible(true);
}
}
这是我关于JFrame的init的示例:
public class Windows{
public static void main(String args[]){
SJFrame window = new SJFrame("NEWNEWNEW");
window.init();
}
}
public class SJFrame extends JFrame(){
public SJFrame(String s){
super(s);
}
void init(){
Container panel = this.getContentPane();
panel.setBackground(Color.green);
panel.setLayout(new GridLayout(5,1));
JLabel jl1 = new JLabel("UserName");
JLabel jl2 = new JLabel("PassWord");
this.add(jl1);
this.add(jl2);
this.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE) ;
this.setSize(300,100);
this.pack();
this.setVisible(true);
}
}
你所说的初始化帧是什么意思?另外,为什么变量和类使用相同的名称(Panel Panel=new Panel()
)?我想在那里设计窗口,这样我就初始化了名为Panel的类中的框架。初始化框架是什么意思?另外,为什么变量和类使用相同的名称(Panel Panel=new Panel()
)?我想在那里设计窗口,这样我就初始化了名为Panel的类中的框架,但是如果我想从Panel类中添加它们,我该怎么做呢?当你在Panel类中时,你可以创建每个元素,并按照我在下面写的那样添加它,但是不要写panel.add(component)
只使用add(component)
或this.add(component)
[因为你已经在对象中]例如:public void init(){JButton new_button=new JButton(“Press”);this.add(new_button);JTextArea new_text_text\u area=new JTextArea(“text”);add(new_text_area);}
但是如果我想从Panel类中添加它们,我该怎么做呢?当你在Panel类中时,你可以创建每个元素,并像我在下面写的那样添加它,但是不要编写Panel.add(component)
只使用add(component)
或者this.add(component)
[因为你已经在对象中]例如:public void init(){JButton new_button=new JButton(“Press”);this.add(new_button);JTextArea new_text_area=new JTextArea(“text”);add(new_text_area);}