我不知道如果我只是沒有得到這兩件事情,但從我收集的東西,JFrame只是一個大開放的盒子,所以我想要做的就是打開那個大盒子會說紅色,然後我做了一個JPanel,我假設它是位於JFRAME頂部的東西,我試圖讓它變成灰色,那麼我怎樣才能得到一個紅色的框架,在左邊有一條灰色條帶。我還試圖將這些按鈕垂直放置在灰色的JPanel上,並且如果可能的話,它們全都被拉伸到JPanel的寬度。JFrame和JPanel分離問題?
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
public class Board extends JFrame implements MouseListener,ActionListener
{
public int x1, y1, x2, y2;
public Board()
{
JFrame frame = new JFrame();
frame.setSize(1200, 800);
Container con = frame.getContentPane();
con.setBackground(Color.RED);
addMouseListener(this);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton clear = new JButton("Clear");
clear.addActionListener(this);
JButton emptyR = new JButton("Empty Rectangle");
emptyR.addActionListener(this);
JPanel menu = new JPanel();
menu.setSize(200, 500);
BoxLayout layout = new BoxLayout(menu, BoxLayout.Y_AXIS);
menu.setLayout(layout);
menu.add(clear);
menu.add(emptyR);
//menu.setBackground(Color.black);
frame.add(menu);
JMenuBar menuBar = new JMenuBar();
frame.setJMenuBar(menuBar);
JMenu help = new JMenu("Help");
menuBar.add(help);
JMenuItem about = new JMenuItem("About");
help.add(about);
about.addActionListener(this);
}
public void mouseExited(MouseEvent evt){}
public void mouseEntered(MouseEvent evt){}
public void mouseClicked(MouseEvent evt){}
public void mousePressed(MouseEvent evt)
{
x1 = evt.getX();
y1= evt.getY();
}
public void mouseReleased(MouseEvent evt)
{
x2 =evt.getX();
y2=evt.getY();
}
public void actionPerformed(ActionEvent e)
{
}
}
發佈你的[SSCCE](http://sscce.org),演示問題 – camickr