如何将JPanel添加到JFrame?这让我很困惑。我想将JPanel添加到JFrame。我已经尝试了各种各样的事情,包括扩展,但我无法让它发挥作用。
import javax.swing.JOptionPane;
import java.awt.Color;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class events {
public static void main (String args[]) {
Time timeObject = new Time();
JFrame mainJFrame;
mainJFrame = new JFrame();
mainJFrame.setLayout(BorderLayout());
mainJFrame.setVisible(true);
mainJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainJFrame.setSize(600,400);
mainJFrame.setVisible(true);
mainJFrame.setLayout(new BorderLayout());
mainJFrame.setTitle("Travel Agent System");
mainJFrame.setBackground(Color.BLUE);
timeObject.selectButton();
}
}
import javax.swing.JOptionPane;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Time{
public static void selectButton()
JButton timeButton = new JButton("Time");
JButton moneyButton = new JButton("Money");
JButton hotelButton = new JButton("Hotel");
JButton exitButton = new JButton("Exit");
JPanel buttonPanel = new JPanel();
buttonPanel.setBackground(Color.RED);
buttonPanel.add(timeButton,moneyButtons,hotelButton,exitButton);
}
答案 0 :(得分:11)
查看此来源。请注意评论。
import javax.swing.JOptionPane;
import java.awt.Color;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class events {
public static void main (String args[]) {
Time timeObject = new Time();
JFrame mainJFrame;
mainJFrame = new JFrame();
// Coding by magic!
//mainJFrame.setLayout(BorderLayout());
mainJFrame.setLayout(new BorderLayout());
mainJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// don't do this, just call pack() later
//mainJFrame.setSize(600,400);
mainJFrame.setLayout(new BorderLayout());
mainJFrame.setTitle("Travel Agent System");
mainJFrame.setBackground(Color.BLUE);
timeObject.selectButton();
mainJFrame.add(timeObject.getGUI());
mainJFrame.pack();
// should be last.
mainJFrame.setVisible(true);
}
}
class Time {
private JPanel buttonPanel;
// don't use static unless necessary - it is not necessary.
//public static void selectButton() {
public void selectButton() {
JButton timeButton = new JButton("Time");
JButton moneyButton = new JButton("Money");
JButton hotelButton = new JButton("Hotel");
JButton exitButton = new JButton("Exit");
buttonPanel = new JPanel();
buttonPanel.setBackground(Color.RED);
buttonPanel.add(timeButton);
buttonPanel.add(moneyButton);
buttonPanel.add(hotelButton);
buttonPanel.add(exitButton);
}
public JComponent getGUI() {
return buttonPanel;
}
}
答案 1 :(得分:3)
将JFrame对象传递给selectButton():
timeObject.selectButton(mainJFrame);
然后使用该JFrame对象向其添加JPanel。
public static void selectButton(JFrame frame)
{
JButton timeButton = new JButton("Time");
JButton moneyButton = new JButton("Money");
JButton hotelButton = new JButton("Hotel");
JButton exitButton = new JButton("Exit");
JPanel buttonPanel = new JPanel();
buttonPanel.setBackground(Color.RED);
buttonPanel.add(timeButton,moneyButtons,hotelButton,exitButton);
frame.getContentPane().add(buttonPanel,BorderLayout.CENTER); // i've added to CENTER.
}
这会将JPanel添加到JFrame的CENTER。
答案 2 :(得分:0)
这是一个很好的例子,它实现了一个JFrame类,然后将JPanels添加到JFrame和其他JPanels。