Byt*_* Me 2 java swing components jframe abstract
因此,对于一些作业,我必须创建一个代表余额的条形图.只是为了熟悉图形/组件,我只想在屏幕上放置两个盒子.似乎第一个盒子是"绘制"然后第二个盒子才会覆盖它?这是两个班级.
主类 - BalanceChart.java
package balancechart;
import javax.swing.*;
public class BalanceChart {
Double[] Props = new Double[6];
public static void main(String[] args) {
JFrame f = new JFrame("Balance Chart");
f.setSize(500, 500);
f.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE);
ChartComponent ccOne = new ChartComponent(50, 50, 100, 200);
ChartComponent ccTwo = new ChartComponent(10, 10, 10, 10);
f.add(ccOne);
f.add(ccTwo);
f.setVisible(true);
}
private void getProps(){
//ignore
}
}
Run Code Online (Sandbox Code Playgroud)
组件类 - ChartComponent.java
package balancechart;
import javax.swing.*;
import java.awt.*;
public class ChartComponent
extends JComponent {
private int x, y, w, h;
public ChartComponent(int x, int y, int w, int h){
this.x = x;
this.y = y;
this.w = w;
this.h = h;
}
public void paintComponent(Graphics g){
Graphics2D g2 = (Graphics2D) g.create();
g2.setColor(Color.RED);
g2.fillRect(w, h, x, y);
}
}
Run Code Online (Sandbox Code Playgroud)
我想这与我为JFrame提供的2个添加程序有关,我只是不确定如何去做.
提前致谢
您需要运行而不是步行到最近的布局管理器教程.在那里你会发现JFrame的contentPane默认使用BorderLayout,当你将一个组件添加到BorderLayout-using容器而没有额外的常量时,它默认添加到BorderLayout.CENTER位置,覆盖之前添加的任何内容.
解决方案是创建另一个使用选择布局的JPanel,甚至可以将其保留为JPanel的默认FlowLayout,将组件添加到其中,然后将该JPanel添加到JFrame.
即
ChartComponent ccOne = new ChartComponent(50, 50, 100, 200);
ChartComponent ccTwo = new ChartComponent(10, 10, 10, 10);
JPanel container = new JPanel(); // JPanel uses FlowLayout by default
container.add(ccOne);
container.add(ccTwo);
f.add(container, BorderLayout.CENTER); // just to be clear
f.pack();
f.setVisible(true);
Run Code Online (Sandbox Code Playgroud)
请查看本教程链接: 在容器中布置组件
编辑
正如MadProgrammer指出的那样,你在这里遇到了另一个问题:
public void paintComponent(Graphics g){
Graphics2D g2 = (Graphics2D) g.create();
g2.setColor(Color.RED);
g2.fillRect(w, h, x, y);
}
Run Code Online (Sandbox Code Playgroud)
因为你没有调用超级方法,因此在需要时不会清除旧图像,如果你试图通过在Swing中改变你的w,h,x或y来做一些动画,这将非常重要计时器.要解决这个问题,请务必调用super的方法.此外,的paintComponent应该是protected没有public的,你应该使用@Override注释,以确保你正确地覆盖方法.如果你绝对需要创建一个新的Graphics上下文,那么你应该在完成后处理它,以免耗尽资源.不要不处置由JVM(给你一个图形上下文的,如一个传递给你的paintComponent(...)方法的参数,因为这会产生有害的副作用:
@Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g.create();
g2.setColor(Color.RED);
g2.fillRect(w, h, x, y);
g2.dispose(); // only do this if you create your own Graphics context
}
Run Code Online (Sandbox Code Playgroud)
编辑2
此外,您应该养成尝试不设置任何大小的习惯,而是让GUI的组件和布局管理器在被要求时设置自己的大小,因为这将导致更令人愉悦和灵活的GUI程序.
编辑3
您在评论中说明:
我修复了代码,但似乎没有任何地方出现2个红色方块,窗口的启动范围非常小.我还有很多学习要做,我不明白为什么增加的代码会这样做.
你的问题是你的JComponent,ChartComponent的默认preferredSize将是0,0或1,1; 我忘了哪一个,但没关系,因为在任何一种情况下,组件都不会大到可以看到.要解决此问题,请为您的类提供一个getPreferredSize()方法覆盖,以帮助设置其大小.例如,:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.*;
public class BalanceChart {
Double[] Props = new Double[6];
public static void main(String[] args) {
JFrame f = new JFrame("Balance Chart");
// f.setSize(500, 500);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ChartComponent ccOne = new ChartComponent(50, 50, 100, 200);
ChartComponent ccTwo = new ChartComponent(10, 10, 10, 10);
JPanel container = new JPanel();
container.add(ccOne);
container.add(ccTwo);
f.add(container);
f.pack();
f.setLocationByPlatform(true);
f.setVisible(true);
}
private void getProps() {
// ignore
}
}
class ChartComponent extends JComponent {
private static final int PREF_W = 400;
private static final int PREF_H = PREF_W;
private int x, y, w, h;
public ChartComponent(int x, int y, int w, int h) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
}
@Override
public Dimension getPreferredSize() {
return new Dimension(PREF_W, PREF_H);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g.create();
g2.setColor(Color.RED);
g2.fillRect(w, h, x, y);
g2.dispose();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1215 次 |
| 最近记录: |