JFrame 和 JPanel 分离有问题吗?

发布于 12-07 07:22 字数 2183 浏览 2 评论 0原文

我不知道如果我只是不明白这两件事是什么,但从我收集到的信息来看,JFrame 只是一个打开的大盒子,所以我想做的是让那个打开的大盒子显示为红色,然后之后我正在制作一个 JPanel,我认为它是位于 JFRAME 顶部的东西,我试图将其变为灰色,那么我怎样才能获得一个红色框架,左侧有一条灰色条带。我还尝试将这些按钮沿着灰色 JPanel 垂直放置,如果可能的话,将它们全部拉伸到 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) 
{


}
}

I cant tell If I'm just not getting what these 2 things are but from what I gathered, the JFrame is just a big open box, So what I am trying to do is have that big box that opens will be say Red, then after that I'm making a JPanel which I assume is something that sits on top of the JFRAME, I'm trying to get this to be GRAY, So How can I get a red frame, with a gray strip on the left. I'm also trying to put these buttons vertically along the gray JPanel, and if at all possible them all to be stretched to the width of the 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) 
{


}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷情2024-12-14 07:22:17

乍一看,一旦扩展 JFrame,您就自动拥有了一个 JFrame。 Board JFrame,框架不是必需的。研究教程,特别是 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);
       ...

这是来自 NetBeans 的通用 JSplitPane 方法:

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) 配置为 JFrame 宽度的 1/3。 jSplitPane1.setEnabled(false) 使分割固定。

On a first look, you have automatically a JFrame as soon as you extend JFrame. Board is a JFrame and frame is not necessary. Study the tutorial, especially the Swing section.

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

should rather be:

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

Here is a generic JSplitPane approach fresh from 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
}

You should configure the setDividerLocation(n) to be 1/3 of your JFrame width. The jSplitPane1.setEnabled(false) makes the division fixed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文