简单的父子Java应用程序
我希望创建一个简单的Java应用程序,它应该在黄色框架上绘制两个正方形——红色和蓝色 appln包含一个窗口(JFrame),其中添加了一个contentPane(JPanel)作为子窗口。我在小时候已经将这两个正方形添加到了JFrame中简单的父子Java应用程序,java,swing,Java,Swing,我希望创建一个简单的Java应用程序,它应该在黄色框架上绘制两个正方形——红色和蓝色 appln包含一个窗口(JFrame),其中添加了一个contentPane(JPanel)作为子窗口。我在小时候已经将这两个正方形添加到了JFrame中 /** Squares' container ------------------ This file contiains 2 small squares - red and blue. The user can drag these squares in
/**
Squares' container
------------------
This file contiains 2 small squares - red and blue. The user can drag
these squares in the panel. If the user drags these squares out of the
panel, the square is lost forever.
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class sqsCont {
public static void main(String[] args) {
JFrame window = new JFrame();
window.setVisible(true);
sqsContPanel myPanel = new sqsContPanel();
window.setBounds(0,0,500,250);
window.getContentPane().add(myPanel);
window.setDefaultCloseOperation(window.EXIT_ON_CLOSE);
}
}
/**
Contains 2 squares red and blue side-by-side on
*/
class sqsContPanel extends JPanel{
mySquare redSq;
mySquare blueSq;
public sqsContPanel() {
//setBounds(0,0,500,250);
redSq = new mySquare(Color.RED);
blueSq = new mySquare(Color.BLUE);
add(redSq);
add(blueSq);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
setBackground(Color.YELLOW);
}
}
/**
Squares
*/
class mySquare extends JComponent{
int myWidth = 50;
int myHeight = 50;
Color myColor;
/**
myColor = color of the square
*/
public mySquare(Color myColor) {
this.myColor = myColor;
if(myColor == Color.RED) {
setBounds(10,10,10+myWidth,10+myHeight);
} else {
setBounds(20+myWidth,10,10+myWidth,10+myHeight);
}
setVisible(true);
}
public void paintComponent(Graphics g){
super.paintComponent(g);
if (myColor == Color.RED){
setBackground(Color.RED);
g.setColor(Color.RED);
g.fillRect(10,10,10+myWidth,10+myHeight);
} else {
setBackground(Color.BLUE);
g.setColor(Color.BLUE);
g.fillRect(20+myWidth,10,10+myWidth,10+myHeight);
}
}
}
该代码生成带有黄色边框的窗口。但是,正方形不可见
有人能发现我在这段代码中缺少了什么,或者我应该做些什么来让这个应用程序正常工作
谢谢您应该在AWT事件调度线程(EDT)中更改GUI,包括添加组件 在您的情况下,这很简单:
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() { public void run() {
JFrame window = new JFrame();
window.setVisible(true);
sqsContPanel myPanel = new sqsContPanel();
window.setBounds(0,0,500,250);
window.getContentPane().add(myPanel);
window.setDefaultCloseOperation(window.EXIT_ON_CLOSE);
}});
}
顺便说一句,我将首先添加组件和设置,并且只有在最后一个操作使用
setVisible(true)
Swing和AWT时,才使用布局管理器来确定组件放在容器中的位置(请参阅)。在您的情况下,您似乎试图通过调用setBounds手动放置组件。要使其正常工作,您必须替换面板的默认布局管理器,如下所示:
public sqsContPanel() {
setLayout(null);
....
g.fillRect(20 + myWidth, 10, 10 + myWidth, 10 + myHeight);
一旦你做到了这一点,那么你的正方形组件将在你想要的地方。你将无法看到你的蓝色方块,因为背景颜色从未被涂过。这是因为g.fillRect(…)使用当前组件的局部坐标。你这样称呼它:
public sqsContPanel() {
setLayout(null);
....
g.fillRect(20 + myWidth, 10, 10 + myWidth, 10 + myHeight);
20+myWidth=70。组件的宽度为60。因此,什么也不会被画出来。您可以使用此更简单的版本替换mySquare类中的paintComponent来修复此问题:
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(myColor);
g.fillRect(0, 0, 10 + myWidth, 10 + myHeight);
}
样式说明:在Java中,惯例是所有类名都以大写字母开头。所以mySquare应该是mySquare。感谢您耐心地解决我的问题:)感谢Russ和Paulo。顺便说一句,我现在了解了flowlayout。为此,我删除了[code]setbounds()[\code]并在MySquares类中添加了[code]setPreferredSize(新维度(myWidth,myHeight))[\code]。@Edwards:对于内联代码,请使用反勾(```),然后将其格式化为源代码(而不是解释)。