2016-11-25 60 views
-1

我正在研究一个基于用户给出的输入绘制线性函数的程序。 我设法创建了一个绘制“线条”(许多点)的方法。Java,利用特定的JPanel

正如你在屏幕截图中看到的那样,右侧有一些空间。我想添加一些JButton,JLabels和JTextFields,以便用户可以输入该函数的数据。

Screenshot of the program

但如果我增加一些Jbutton将或一些Jlabel之下,他们将不会在右侧显示。任何人都可以向我解释这种行为的原因吗?我将上传没有JButton的版本的源代码。 谢谢你们的帮助!

package projekt; 

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

public class ProjectFunction extends JFrame { 


    public ProjectFunction() { 


     setLayout(new BorderLayout()); 


     setSize(1900, 1000); 
     setTitle("First Test"); 
     setVisible(true); 
     setDefaultCloseOperation(EXIT_ON_CLOSE); 
    } 


    public void paint(Graphics g){ 
     g.setColor(new Color(204, 204, 204)); 
     g.drawLine(0, 900, 1000, 900); 
     g.drawLine(0, 800, 1000, 800); 
     g.drawLine(0, 700, 1000, 700); 
     g.drawLine(0, 600, 1000, 600); 
     g.drawLine(0, 400, 1000, 400); 
     g.drawLine(0, 300, 1000, 300); 
     g.drawLine(0, 200, 1000, 200); 
     g.drawLine(0, 100, 1000, 100); 
     g.drawLine(100, 0, 100, 1000); 
     g.drawLine(200, 0, 200, 1000); 
     g.drawLine(300, 0, 300, 1000); 
     g.drawLine(400, 0, 400, 1000); 
     g.drawLine(600, 0, 600, 1000); 
     g.drawLine(700, 0, 700, 1000); 
     g.drawLine(800, 0, 800, 1000); 
     g.drawLine(900, 0, 900, 1000); 
     g.setColor(Color.BLACK); 
     g.drawRect(0, 500, 1000, 1); 
     g.drawRect(500, 0, 1, 1000); 
     g.setColor(Color.RED); 
     linear(0.25, 1, g); 
     g.setColor(Color.BLUE); 
     linear(-3, -2.5, g); 

    } 


    public void linear(double s, double c, Graphics g) { 
     int Anzpunkte = 0; 
     c = c * 100; 
     int x = 500, y = 500 - (int) c; 
     g.drawOval(x, y, 2, 2); 
     y = y - (int) s; 
     double abtrag = s - (int) s; 
     System.out.println("Punkt X-Achse Y-Achse Abtrag Steigung"); 
     Anzpunkte++; 
     System.out.println("" + Anzpunkte + "  " + x + "  " + y + "  " + abtrag + " " + s); 
     while (x < 1000 && y < 1000 && x > 0 && y > 0) { 
      x++; 
      g.drawOval(x, y, 2, 2); 
      Anzpunkte++; 
      System.out.println("" + Anzpunkte + "  " + x + "  " + y + "  " + abtrag + " " + s); 
      if (abtrag >= 1 || abtrag <= -1) { 
       y = y - (int) s; 
       y = y - (int) abtrag; 
       abtrag = s - (int) s; 
      } else { 
       y = y - (int) s; 
       abtrag = abtrag + s - (int) s; 
      } 
     } 
     x = 500; 
     y = 500 - (int) c; 
     while (x < 1000 && y < 1000 && x > 0 && y > 0) { 
      x--; 
      g.drawOval(x, y, 2, 2); 
      Anzpunkte++; 
      System.out.println("" + Anzpunkte + "  " + x + "  " + y + "  " + abtrag + " " + s); 
      if (abtrag >= 1 || abtrag <= -1) { 
       y = y + (int) s; 
       y = y + (int) abtrag; 
       abtrag = s - (int) s; 
      } else { 
       y = y + (int) s; 
       abtrag = abtrag + s - (int) s; 
      } 
     } 


    } 


    public static void main(String[] args) { 
     ProjectFunction p = new ProjectFunction(); 


    } 

} 
+0

没有代码在您发布的任何地方添加任何按钮或标签组件。 –

+0

欢迎来到Stack Overflow!请查看我们的[SO问题清单](http://meta.stackoverflow.com/questions/260648/stack-overflow-question-checklist)来帮助你提出一个好问题,从而得到一个很好的答案。 –

回答

2

如果你想添加按钮的权利,你会想了解和使用布局管理器。一个关键的概念是,你可以通过嵌套JPanels来有效地嵌套布局,每个布局使用它自己的布局。例如,如果我们在JPanel中完成了绘图(我们建议这样做而不是直接在JFrame中绘制,我们可以将此JPanel放置在另一个在BorderLayOut.CENTER位置使用BorderLayout的JPanel中,然后我们可以添加另一个JPanel使用网格布局,以按钮添加到该外JPanel的右侧

也许是更好地只显示一个例子:

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.Graphics; 
import java.awt.GridLayout; 

import javax.swing.*; 

public class ProjectFunctionTest { 
    public static void main(String[] args) { 
     SwingUtilities.invokeLater(() -> { 
      JFrame mainFrame = new JFrame("Project Function"); 
      mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      mainFrame.add(new ProjFunctMainPanel()); 
      mainFrame.pack(); 
      mainFrame.setLocationRelativeTo(null); 
      // mainFrame.setExtendedState(JFrame.MAXIMIZED_BOTH); 
      mainFrame.setVisible(true); 
     }); 
    } 
} 

class ProjFunctMainPanel extends JPanel { 
    private static final String[] BUTTON_TEXTS = {"Open", "Save", "Edit", "Exit"}; 
    private ProjFunctDrawingPanel drawingPanel = new ProjFunctDrawingPanel(); 

    public ProjFunctMainPanel() { 
     // an inner jpanel to hold our jbuttons and uses grid layout 
     JPanel buttonPanel = new JPanel(new GridLayout(0, 1, 5, 5)); 
     for (String btnText : BUTTON_TEXTS) { 
      buttonPanel.add(new JButton(btnText)); 
     } 

     // a wrapper jpanel to hold the button panel above at its top 
     // so the buttons are loaded top-right 
     JPanel rightPanel = new JPanel(new BorderLayout()); 
     rightPanel.add(buttonPanel, BorderLayout.PAGE_START); 

     // make outer panel use borderlayout 
     setLayout(new BorderLayout()); 
     add(drawingPanel, BorderLayout.CENTER); // add drawing to the center 
     add(rightPanel, BorderLayout.LINE_END); // and wrapper panel with buttons to the right 
    } 
} 

class ProjFunctDrawingPanel extends JPanel { 
    private static final int PANEL_W = 1000; 
    private static final int PANEL_H = 900; 

    public ProjFunctDrawingPanel() { 
     setBorder(BorderFactory.createLineBorder(Color.BLUE)); 
    } 

    // this will set the preferred size of the jpanel to be one that fits the image 
    @Override 
    public Dimension getPreferredSize() { 
     if (isPreferredSizeSet()) { 
      return super.getPreferredSize();    
     } else { 
      return new Dimension(PANEL_W, PANEL_H); 
     } 
    } 

    // draw in a JPanel's paintComponent method 
    @Override 
    protected void paintComponent(Graphics g) { 
     super.paintComponent(g); // don't forget to call the super's method 
     g.setColor(new Color(204, 204, 204)); 
     g.drawLine(0, 900, 1000, 900); 
     g.drawLine(0, 800, 1000, 800); 
     g.drawLine(0, 700, 1000, 700); 
     g.drawLine(0, 600, 1000, 600); 
     g.drawLine(0, 400, 1000, 400); 
     g.drawLine(0, 300, 1000, 300); 
     g.drawLine(0, 200, 1000, 200); 
     g.drawLine(0, 100, 1000, 100); 
     g.drawLine(100, 0, 100, 1000); 
     g.drawLine(200, 0, 200, 1000); 
     g.drawLine(300, 0, 300, 1000); 
     g.drawLine(400, 0, 400, 1000); 
     g.drawLine(600, 0, 600, 1000); 
     g.drawLine(700, 0, 700, 1000); 
     g.drawLine(800, 0, 800, 1000); 
     g.drawLine(900, 0, 900, 1000); 
     g.setColor(Color.BLACK); 
     g.drawRect(0, 500, 1000, 1); 
     g.drawRect(500, 0, 1, 1000); 
     g.setColor(Color.RED); 
     linear(0.25, 1, g); 
     g.setColor(Color.BLUE); 
     linear(-3, -2.5, g); 
    } 

    public void linear(double s, double c, Graphics g) { 
     int Anzpunkte = 0; 
     c = c * 100; 
     int x = 500, y = 500 - (int) c; 
     g.drawOval(x, y, 2, 2); 
     y = y - (int) s; 
     double abtrag = s - (int) s; 
     System.out.println("Punkt X-Achse Y-Achse Abtrag Steigung"); 
     Anzpunkte++; 
     System.out.println("" + Anzpunkte + "  " + x + "  " + y + "  " + abtrag + " " + s); 
     while (x < 1000 && y < 1000 && x > 0 && y > 0) { 
      x++; 
      g.drawOval(x, y, 2, 2); 
      Anzpunkte++; 
      System.out.println("" + Anzpunkte + "  " + x + "  " + y + "  " + abtrag + " " + s); 
      if (abtrag >= 1 || abtrag <= -1) { 
       y = y - (int) s; 
       y = y - (int) abtrag; 
       abtrag = s - (int) s; 
      } else { 
       y = y - (int) s; 
       abtrag = abtrag + s - (int) s; 
      } 
     } 
     x = 500; 
     y = 500 - (int) c; 
     while (x < 1000 && y < 1000 && x > 0 && y > 0) { 
      x--; 
      g.drawOval(x, y, 2, 2); 
      Anzpunkte++; 
      System.out.println("" + Anzpunkte + "  " + x + "  " + y + "  " + abtrag + " " + s); 
      if (abtrag >= 1 || abtrag <= -1) { 
       y = y + (int) s; 
       y = y + (int) abtrag; 
       abtrag = s - (int) s; 
      } else { 
       y = y + (int) s; 
       abtrag = abtrag + s - (int) s; 
      } 
     } 
    } 
} 

在图纸上的说明:你永远不希望直接在JFrame中得出的JFrames是复杂的GUI组件,可以容纳,显示和绘制多个组件,并且绘制其绘制方法可能会导致该功能的风险。另外,它没有paintComponent方法,因此您会失去自动马蒂克双缓冲这是非常重要的,如果你曾经做动画。相反,请在JPanel的paintComponent方法中绘制,并在覆盖中调用超级方法,以便完成家务绘画。

1

您没有到super.paint(g)调用其中框架的组件绘制覆盖paint(...)。如果你先打电话,然后画线,你可以画一些组件。你的下一个问题将是布局。如果您没有使用GUI构建器(我不会推荐,因为我是纯粹主义者; P),您将不得不处理适当的布局,我认为将线条抽屉不放在JFrame本身中是可取的但在JPanel中将放入JFrame中。我也推荐GridBagLayout,但你可能会因为它有点复杂而讨厌我。

如果您使用的GUI生成器...不知道,我从来没有。

1

右侧有一些空间。我想添加一些JButtons,JLabels和JTextFields,以便用户可以输入数据作为功能。

所以后来在您的框架,你需要两个独立的面板添加到框架:

  1. ,你做的风俗画的第一个面板。你为这幅画覆盖paintComponent(..)

  2. 第二面板将包含您的组件

所以基本的代码如下:

JFrame frame = new JFrame(...); 
frame.add(new PaintingPanel(), BorderLayout.CENTER); 
frame.add(new ComponentPanel(), BorderLayout.LINE_END); 

不要做直接在框架上的风俗画。

阅读自定义绘画上的Swing教程部分以获取更多信息和工作示例。

+0

比我的和简要的更好的答案。 1+ –

+0

谁的***表决了这个答案?? –