简体   繁体   English

如何正确将JPanel放到JFrame上?

[英]How can I correctly place a JPanel onto a JFrame?

I'm currently studying Software Engineering at Uni and I'm struggling to come to terms with how to add a JPanel onto a JFrame properly. 我目前正在Uni学习软件工程,我很难接受如何正确地将JPanel添加到JFrame上。 My JPanel has a couple of buttons as well as a JLabel which changes by clicking on one of the buttons and using an ActionListener. 我的JPanel有几个按钮和一个JLabel,通过单击其中一个按钮并使用ActionListener进行更改。

I know there are several ways to do it, and here is what I've been trying but I can't for the life of me figure it out! 我知道有几种方法可以做到这一点,这就是我一直在尝试但我不能为我的生活弄明白!

I know I am doing loads wrong but what is it? 我知道我做错了但是它是什么?

Here is my code: 这是我的代码:

UIPanelOne: UIPanelOne:

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.event.*;


public class UIPanelOne extends JPanel implements ActionListener {

private JButton yes, no, cancel;
private JPanel panel;
private JLabel l1, l2;

UIPanelOne() {

    super();
    setVisible(true);

    //label dec
    panel = new JPanel();

    //buttons
    yes = new JButton("Yes");
    no = new JButton("No");
    cancel = new JButton("Cancel");

    //label dec
    l1 = new JLabel("Hello");

    //button dec
    panel.setLayout(new BorderLayout());
    panel.add(yes);
    panel.add(no);
    panel.add(cancel);
    panel.add(l1);

}

public void actionPerformed(ActionEvent e) {

    if (e.getSource() == yes) {

        l1.setText("OK then!");
    }else if (e.getSource() == no){

        l1.setText("Goodbye then!");


    }else if(e.getSource() == cancel){

        System.exit(0);
    }


}

public JComponent getGUI(){

    return panel;
}
}

UIFrame: UIFrame:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


public class UIFrame extends JFrame{


//constructor
public UIFrame(){

    //layout
    super("Can I help you?");
    setSize(400,600);
    setLayout(new BorderLayout());
    setVisible(true);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    UIPanelOne hello = new UIPanelOne();

    getContentPane().add(hello.getGUI());
   }
}

UITest: UITest:

public class UITest {

public static void main(String[] args){

UIFrame frame = new UIFrame();
frame.pack();

}

}

I know alot of this is probably wrong and I'm a total amateur but I'm hoping to get better with some help! 我知道很多这可能是错的,我是一个完全的业余爱好者,但我希望能在一些帮助下变得更好!

Since your UIPanelOne extends JPanel class you can add your component's on that panel (no need for creation of new panel), create a new instance and pass that instance to add method of JFrame : 由于您的UIPanelOne扩展了JPanel类,您可以在该面板上添加组件(无需创建新面板),创建新实例并传递该实例以添加JFrame方法:

add(new UIPanelOne());

or 要么

UIPanelOne hello = new UIPanelOne();
add(hello,BorderLayout.CENTER);

or 要么

setContentPane(hello);

Avoid extending your classes with swing component's unless you want to define new method's for them (creation of custom component's) or if you wan't to override some of their method's. 避免使用swing组件扩展您的类,除非您想为它们定义新方法(创建自定义组件)或者如果您不想覆盖它们的某些方法。

You don't have to set BorderLayout for JFrame since it's default layout for JFrame (aleady set). 您不必为JFrame设置BorderLayout ,因为它是JFrame (aleady set)的默认布局。

Call setVisible method for JFrame AFTER you add component's. 在添加组件后调用JFrame setVisible方法。

Also, read about Concurrency in Swing 另外,请阅读Swing中的Concurrency

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM