2011-09-29 71 views
1

我不知道如果我只是没有得到这两件事情,但从我收集的东西,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) 
{ 


} 
} 
+0

发布你的[SSCCE](http://sscce.org),演示问题 – camickr

回答

2

第一次看,您在扩展JFrame后立即自动生成一个JFrame。板一个JFrame和框架是没有必要的。研究tutorial,特别是Swing部分。

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); 
    ... 

而应是:

public class Board extends JFrame implements MouseListener,ActionListener { 
    public int x1, y1, x2, y2; 

    public Board() { 
     setSize(1200, 800); 
     Container con = getContentPane(); 
     con.setBackground(Color.RED); 
     addMouseListener(this); 
     setVisible(true); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     ... 

这里是一个通用的方法JSplitPane的新鲜从NetBeans中:

public class NewJFrame1 extends javax.swing.JFrame { 

    /** Creates new form NewJFrame1 */ 
    public NewJFrame1() { 
     initComponents(); 
    } 

    /** This method is called from within the constructor to 
    * initialize the form. 
    * WARNING: Do NOT modify this code. The content of this method is 
    * always regenerated by the Form Editor. 
    */ 
    @SuppressWarnings("unchecked") 
    // <editor-fold defaultstate="collapsed" desc="Generated Code"> 
    private void initComponents() { 

     jSplitPane1 = new javax.swing.JSplitPane(); 
     jPanel1 = new javax.swing.JPanel(); 
     jPanel2 = new javax.swing.JPanel(); 

     setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

     jSplitPane1.setBorder(null); 
     jSplitPane1.setDividerLocation(100); 
     jSplitPane1.setDividerSize(1); 
     jSplitPane1.setEnabled(false); 

     jPanel1.setBackground(new java.awt.Color(153, 153, 153)); 

     javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); 
     jPanel1.setLayout(jPanel1Layout); 
     jPanel1Layout.setHorizontalGroup(
      jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 100, Short.MAX_VALUE) 
     ); 
     jPanel1Layout.setVerticalGroup(
      jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 300, Short.MAX_VALUE) 
     ); 

     jSplitPane1.setLeftComponent(jPanel1); 

     jPanel2.setBackground(new java.awt.Color(255, 0, 0)); 

     javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2); 
     jPanel2.setLayout(jPanel2Layout); 
     jPanel2Layout.setHorizontalGroup(
      jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 299, Short.MAX_VALUE) 
     ); 
     jPanel2Layout.setVerticalGroup(
      jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 300, Short.MAX_VALUE) 
     ); 

     jSplitPane1.setRightComponent(jPanel2); 

     getContentPane().add(jSplitPane1, java.awt.BorderLayout.CENTER); 

     pack(); 
    }// </editor-fold> 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String args[]) { 

     /* Create and display the form */ 
     java.awt.EventQueue.invokeLater(new Runnable() { 

      public void run() { 
       new NewJFrame1().setVisible(true); 
      } 
     }); 
    } 
    // Variables declaration - do not modify 
    private javax.swing.JPanel jPanel1; 
    private javax.swing.JPanel jPanel2; 
    private javax.swing.JSplitPane jSplitPane1; 
    // End of variables declaration 
} 

应该配置setDividerLocation(N)是1/3的JFrame宽度。 jSplitPane1.setEnabled(false)使该分区得到修复。

+0

好吧,我做了这些改变,但我仍然遇到这个问题,当我运行与jpanel没有添加它是红色的是什么框架,然后如果我添加菜单aka jpanel它变成灰色!有任何想法吗?感谢您的提示。 – CMOS

+0

con.setBackground(Color.RED);添加con.setLayout(new FlowLayout());例如。这会给你的ContentPane一个FlowLayout。我不确切知道你在做什么。阅读http://download.oracle.com/javase/tutorial/uiswing/layout/using.html#sizealignment –

+0

我试图得到一个大的矩形,右边的3/4将是红色的,左边的1/4将是灰色与垂直对齐的按钮。 – CMOS