为什么我的JPanel会破坏我的JFrame上的其他组件?

时间:2013-08-04 00:44:31

标签: java swing jpanel paintcomponent dimension

所以我有一个JFrame,它包含一个包含JList组件的JPanel。

然后我为我的paintComponent()添加了另一个JPanel,它也会返回一个维度。

但是当我设置维度的大小时,它会尝试重新定位我的其他JPanel。

以下是paintComponent()的代码:

class drawOnPanel extends JPanel {
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D)g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setColor(Color.BLUE);
    }
    @Override
  public Dimension getPreferredSize(){
    return new Dimension(250, 250);
  }
}

然后我有我的JFrame调用drawOnPanel类:

public static void mainFrame() {
    JFrame f = new MTGSAMPServerReference();
    f.setTitle("MTG SAMP Server Reference Guide");
    f.pack();
    f.setVisible(true);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.add(new drawOnPanel());
    f.setSize(330, 300);
    f.setLocationRelativeTo(null);
}

然后我的JList出现在我的JPanel上:

public void MainMenu() {
    JPanel controls = new JPanel(new BorderLayout(5,5));
    final CardLayout cl = new CardLayout();
    final JPanel panel = new JPanel(cl);
    controls.add(panel);
    this.getContentPane().setLayout(new FlowLayout(FlowLayout.LEADING));
        list = new JList<Object>(mainMenu);
        list.setVisibleRowCount(7);
        select = new JButton("Select");
        exit = new JButton("Exit");
        select.addActionListener(this);
        exit.addActionListener(this);
        controls.add(new JScrollPane(list));
        JPanel basePanel = new JPanel(new GridLayout(0, 1));
        basePanel.add(select);
        basePanel.add(exit);
        controls.add(basePanel, BorderLayout.PAGE_END);
        add(controls);
        refreshFrame();
}

当我尝试在我的paintComponent() JPanel上绘图时,它会绘制,但是我指示的坐标没有正确绘制。

有谁知道为什么会这样?

提前致谢!

编辑:这是我的程序的一些截图。

当我不包含drawOnPanel时,这是我的程序的样子: No drawOnPanel

当我加入drawOnPanel时,这个是我的程序的样子: With drawOnPanel

我只是希望它在JList的右侧绘制,而不移动JList。如您所见,它会调整其他JPanel。

感谢任何和所有帮助!

编辑:这是我的SSCCE

编辑:这是我的SSCCE代码:

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

public final class SSCCE1 extends JFrame implements ActionListener {
    private static final long serialVersionUID = 1L;
    JList list;
    JButton select;
    JButton exit;
    Object[]mainMenu = {"Value 1", "Value 2", "Value 3", "Value 4"};

    public SSCCE1() {
        MainMenu();
    }

    public void MainMenu() {
        JPanel controls = new JPanel(new BorderLayout(5,5));
        final CardLayout cl = new CardLayout();
        final JPanel panel = new JPanel(cl);
        controls.add(panel);
        this.getContentPane().setLayout(new FlowLayout(FlowLayout.LEADING));
            list = new JList<Object>(mainMenu);
            list.setVisibleRowCount(7);
            select = new JButton("Select");
            exit = new JButton("Exit");
            controls.add(new JScrollPane(list));
            JPanel basePanel = new JPanel(new GridLayout(0, 1));
            basePanel.add(select);
            basePanel.add(exit);
            controls.add(basePanel, BorderLayout.PAGE_END);
            add(controls);
            revalidate();
            repaint();
            SSCCE1.this.repaint();
    }

    public void createAndShowGUI() {
        mainFrame();
        SSCCE1.this.repaint();
    }

    public static void mainFrame() {
        JFrame f = new SSCCE1();
        f.setTitle("My SSCCE");
        f.pack();
        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(new drawOnPanel()); // When this is uncommented, it messes with the other JPanel, but when commented, it works fine, but does not allow drawing on the other JFrame.
        f.setExtendedState(JFrame.MAXIMIZED_BOTH);
        f.setLocationRelativeTo(null);
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
            SSCCE1 gui = new SSCCE1();
            gui.createAndShowGUI();
            }
        });
    }
    @Override
    public void actionPerformed(ActionEvent ae) {
    }
}

class drawOnPanel extends JPanel {
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D)g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setColor(Color.BLUE);
    }
  @Override
  public Dimension getPreferredSize(){
    return new Dimension(250, 250);
  }
}

1 个答案:

答案 0 :(得分:3)

在添加,打包和定位后,使setVisible() 最后。完整示例见herehere

f.add(…);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);

附录:这是您示例的变体,没有隐形面板。如果没有令人信服的理由扩展JFrame,只需创建一个并添加您的组件。

image

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

/** @see https://stackoverflow.com/a/18038765/230513 */
public class SSCCE2 {

    private JList list;
    private JButton select;
    private JButton exit;
    private Object[] mainMenu = {"Value 1", "Value 2", "Value 3", "Value 4"};

    public JPanel mainMenu() {
        JPanel controls = new JPanel(new BorderLayout(5, 5));
        list = new JList(mainMenu);
        list.setVisibleRowCount(7);
        select = new JButton("Select");
        exit = new JButton("Exit");
        controls.add(new JScrollPane(list));
        JPanel basePanel = new JPanel(new GridLayout(0, 1));
        basePanel.add(select);
        basePanel.add(exit);
        controls.add(basePanel, BorderLayout.PAGE_END);
        return controls;
    }

    public void createAndShowGUI() {
        JFrame f = new JFrame("My SSCCE");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(new DrawOnPanel());
        f.add(mainMenu(), BorderLayout.WEST);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                SSCCE2 gui = new SSCCE2();
                gui.createAndShowGUI();
            }
        });
    }

    private static class DrawOnPanel extends JPanel {

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(Color.BLUE);
            g.fillRect(0, 0, getWidth(), getHeight());
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(250, 250);
        }
    }
}