简体   繁体   English

如何在Java swing中的拆分面板中绘制

[英]How to paint in a split panel in Java swing

Recently, I've met an issue that the paintComponent function is not invoked in the function, and I found that when I use splitpane function, it will disable the paint function, and gives error: 最近,我遇到了一个问题,即未在函数中调用paintComponent函数,并且我发现当我使用splitpane函数时,它将禁用paint函数,并产生错误:

cannot add to layout: unknown constraint: null 无法添加到布局:未知约束:null

I think the paint function may not be added to the right way, below is my code(partly): 我认为画图功能可能未添加到正确的方式,下面是我的代码(部分):

Class: test 类:测试

public class Test extends JFrame{

    public Test() throws IOException{
        //JFrame jf = new JFrame("my frame");
        this.add(new NewPanel(this));

        this.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        this.setBounds(300,200,1050,600);
        this.setVisible (true);

    }

    public static void main (String[] args) throws IOException{

         Test test   = new Test ();
         test.setTitle("Hello");
          //frame.pack ();
     }
}

Class: NewPanel 类:NewPanel

public class NewPanel extends JPanel{
    public NewPanel(JFrame frame) throws IOException{
          JTabbedPane jTabbedpane = new JTabbedPane();
          JSplitPane splitPane    = new JSplitPane();
          JPanel p1               = new JPanel();

          p1.setLayout(null);
          p2.setLayout(new FlowLayout());

          splitPane.setOneTouchExpandable(true);
          splitPane.setContinuousLayout(true);
          //splitPane.setPreferredSize(new Dimension (250,500));
          splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
          splitPane.setLeftComponent(p1);
          splitPane.setRightComponent (p2);
          splitPane.setDividerSize(3);
          splitPane.setDividerLocation(250);  //balance two panels width

          jTabbedpane.addTab("ABC",p2);
          jTabbedpane.addTab("AB",p3);
          jTabbedpane.addTab("AC",p4);
          jTabbedpane.addTab("BC",p5);

          frame.setContentPane(splitPane);
          frame.add(jTabbedpane);

          }
    }
     public void paintComponent(Graphics g){
        super.paint(g);
        g.setColor(Color.BLUE);
        g.drawLine(303, 90, 303, 200);
        g.drawLine(583, 90, 583, 200);
        g.drawLine(863, 90, 863, 200);
    }
}

When I comment frame.add(jTabbedpane) ,the line could be drawn in the panel, BUT it is only available in one panel, I cannot draw it into another split panel, I don't know why.. And when I uncomment frame.add(jTabbedpane) , it pops up the above mentioned error. 当我评论frame.add(jTabbedpane) ,该行可能会在面板上绘制, 它只是在一个面板可用,我无法将其拉入另一个分裂面板,我不知道为什么。而当我取消frame.add(jTabbedpane) ,它会弹出上述错误。

Your UI assembly doesn't make sense. 您的UI程序集没有意义。 You're calling 'setContentPane' to the splitpane, which is sort-of OK (but unusual), but then you're calling add() to the frame, which tries to then add something else to the contentPane (the JSplitPane). 您正在对拆分窗格调用“ setContentPane”,这是可以的(但不常见),但是随后您在框架中调用了add(),从而试图将其他内容添加到contentPane(JSplitPane)中。 You should either add the JTabbedPane to the SplitPane before adding the splitPane to the JPanel, or set up your layout differently. 您应该在将splitPane添加到JPanel之前将JTabbedPane添加到SplitPane,或者以其他方式设置布局。

//These don't make sense together.
frame.setContentPane(splitPane);
frame.add(jTabbedpane);

Your second question about drawing the blue line is more complicated. 关于绘制蓝线的第二个问题更加复杂。 You're doing a bunch of crazy stuff - you're creating a NewPanel and trying to add it to the JFrame , but then you're setting the contentPane of the JFrame to a different component later. 您正在做很多疯狂的事情-创建一个NewPanel并尝试将其添加到JFrame ,但是稍后您将JFrame的contentPane设置为其他组件。 You need to go through the Swing Tutorial and lay out your UI better. 您需要阅读Swing教程并更好地布局UI。

I think the paint function may not be added to the right way, 我认为画图功能可能无法正确添加,

 public void paintComponent(Graphics g){
    super.paint(g);

You are overriding paintComponent(...) , so why are you calling super.paint(...) ? 您正在重写paintComponent(...) ,那么为什么要调用super.paint(...)

Start by reading the Swing Tutorial for Swing basics. 首先阅读有关Swing基础知识的Swing教程 All sections in the tutorial have working examples you can download and test. 本教程中的所有部分都包含可以下载和测试的工作示例。

So you might start with: 因此,您可以从以下内容开始:

  1. How to Use Split Panes - it will show you how to add a split pane to a frame 如何使用拆分窗格-它将向您展示如何将拆分窗格添加到框架
  2. Performing Custom Painting - it will explain how painting works and show how to override the paintComponent(...) method. 执行自定义绘画-将解释绘画的工作原理,并说明如何覆盖paintComponent(...)方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM