java-为什么我的JPanel与我的JFrame上的其他组件搞混了?

所以我有一个JFrame,其中包含一个JPanel,其中包含一个JList组件.

然后,我的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);
}

然后我的JPanel上有我的JList:

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时程序的外观:

这是我包含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);
  }
}

解决方法:

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

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

附录:这是您的示例的一种变体,没有隐形面板.无需扩展JFrame的必要理由,只需创建一个并添加您的组件即可.

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

/** @see https://*.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);
        }
    }
}
上一篇:离散化的认识


下一篇:500. 键盘行