【问题标题】:Drawing to a GUI From the main using another class In Java使用Java中的另一个类从主绘制到GUI
【发布时间】:2015-12-25 03:48:38
【问题描述】:

我是一个初级程序员,所以我不知道所有的词汇,但我了解一些java的基础知识。

所以我正在尝试使用另一个类从主界面绘制图形用户界面。我知道我不是很具体,但这是我的代码,我将尝试解释我正在尝试做什么。

这是我的主线

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

public class ThisMain {


public static void main(String[] args) {
    // TODO Auto-generated method stub
    JFrame theGUI = new JFrame();
    theGUI.setTitle("GUI Program");
    theGUI.setSize(600, 400);
    theGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    ColorPanel panel = new ColorPanel(Color.white);
    Container pane = theGUI.getContentPane();
    pane.add(panel);
    theGUI.setVisible(true);



    }
}

这是我的另一堂课

import javax.swing.*;
import java.awt.*;
public class ColorPanel extends JPanel {
    public ColorPanel(Color backColor){
    setBackground(backColor);

}

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

}
}

我正在尝试使用这条线

 ColorPanel panel = new ColorPanel(Color.white);

或者类似的东西来使用类似的东西

 drawRect();

在 main 中并让它在 GUI 中绘制。

这是我认为最接近工作的代码

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

public class ThisMain {


    public static void main(String[] args) {
        // TODO Auto-generated method stub
        JFrame theGUI = new JFrame();
        theGUI.setTitle("GUI Program");
        theGUI.setSize(600, 400);
        theGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        //I'm trying to draw a string in the JFrame using ColorPanel but i'm            Trying to do it from the main
        ColorPanel panel = new ColorPanel(){
            public void paintComponent(Graphics g){
                super.paintComponent(g);
                //This is the line I need to work using the ColorPanel in anyway
                g.drawString("Hello world!", 20, 20);
        };
        Container pane = theGUI.getContentPane();
        //The errors occur here
        pane.add(panel);
        theGUI.setVisible(true);


    //and on these brackets
    }
}

【问题讨论】:

  • "Or something like it to use things like... " -- 你想在 JPanel 上画什么?请尽可能详细,因为大多数解决方案都取决于这些细节。
  • 老实说,我不知道我希望有人知道如何用那条线做某事,因为我只知道我应该使用那条线
  • 我不知道你想要达到什么目标,也不知道为什么你所拥有的东西对你不起作用,就目前而言,你没有(真的)问一个问题,或者更多点,问了一个我们可以回答的问题
  • 是的,我不知道,但谢谢
  • “老实说,我不知道我希望有人知道如何处理那条线,因为我只知道我应该使用那条线” -用什么做什么?也许你应该看看Painting in AWT and SwingPerforming Custom Painting2D Graphics

标签: java swing user-interface main


【解决方案1】:

我不太确定你到底想做什么(你没有回复我对你的问题的评论以供澄清),但我会:

  • 给你的 ColorPanel 一个List<Shape>
  • 给它一个public void addShape(Shape s) 方法,允许外部类将形状插入这个List,然后调用repaint()
  • 然后在 ColorPanel 的 paintComponent 方法中,遍历 List,使用您的 Graphics2D 对象绘制每个 Shape 项。

请注意,Shape 是一个接口,因此您的 List 可以包含 Ellipse2D 对象、Rectangle2D 对象、Line2D 对象、Path2D 对象以及实现该接口的大量其他对象。此外,如果您想将每个形状与颜色相关联,您可以将项目存储在 Map<Shape, Color> 中。

【讨论】:

    【解决方案2】:

    无法编译的原因是这行造成的:

    ColorPanel 面板 = new ColorPanel(Color.white);

    因为类 ColorPanel 不包含在 swing 库中,所以您必须对其进行编码。此代码扩展了 JPanel 并包含一个采用 Color 参数的构造函数。构造函数在面板被实例化并设置其背景颜色时运行:

    import javax.swing.*;
    import java.awt.*;
    
    public class ColorPanel extends JPanel {
       public ColorPanel(Color backColor) {
        setBackground(backColor);
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-21
      • 1970-01-01
      • 1970-01-01
      • 2013-10-11
      • 2013-01-18
      相关资源
      最近更新 更多