【发布时间】: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 Swing、Performing Custom Painting和2D Graphics
标签: java swing user-interface main