我正在尝试在java swing中创建一个窗口。
首先我创建一个 JFrame ,在其中我设置了一个:
MenuBar,ToolBar,DrawingSurface(扩展JPanel)
在 DrawingSurface 我覆盖方法 paintComponent(),所以我可以在Panel内动态绘制,问题是我尝试在面板中添加两个标签(调用两次到 public int registerInput(String name,String comp1,String comp2))只有一个显示,我尝试打印位置大小...进行debuging,我发现绘图开始正常但经过几次迭代后第二个标签突然变得太大了,我想这就是为什么它没有显示出来
这是DrawingSurface类:
import java.awt.Dimension;
import java.awt.Graphics;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
public class DrawingSurface extends JPanel{
class DrawingObjects {
public String name;
public String comp1;
public String comp2;
float totalTcd;
float totalTpd;
int numberOfChanges;
JLabel label;
float surfaceY;
float surfaceX;
}
private final int WidthFactor = 10;
private final int HeightFactor = 5;
private ArrayList<DrawingObjects> drawingObjects = new ArrayList<DrawingObjects>();
DrawingSurface(){}
public int registerInput(String name,String comp1,String comp2) {
for(DrawingObjects dO :drawingObjects){
if (dO.name.equals(name)){
System.out.println("Object already exist in Drawing surface");
return -1;
}
}
DrawingObjects newObj = new DrawingObjects();
newObj.name = name;
newObj.comp1 = comp1;
newObj.comp2 = comp2;
//newObj.totalTcd = (float) tree.tcdComputing(tree.vertexes.get(comp1));
//newObj.totalTpd = (float) tree.tpdComputing(tree.vertexes.get(comp1));
newObj.numberOfChanges = 0;
newObj.label = new JLabel(newObj.name,SwingConstants.CENTER);
newObj.label.setBorder(BorderFactory.createTitledBorder(""));
int tmpWidth = (int) (getSize().getWidth()/WidthFactor);
int tmpHeight = (int) (getSize().getHeight()/HeightFactor);
newObj.label.setSize(tmpWidth,tmpHeight);
System.out.println("Label "+(getSize().getWidth()/WidthFactor)+ " , "+(getSize().getHeight()/HeightFactor));
System.out.println("Surface Height width "+getSize().getHeight()+" , "+getSize().getWidth());
newObj.label.setMinimumSize(new Dimension(tmpWidth, tmpHeight));
newObj.label.setMaximumSize(new Dimension(tmpWidth, tmpHeight));
drawingObjects.add(newObj);
add(newObj.label);
return 0;
}
public void unregisterInput(String name) {
for(DrawingObjects dO :drawingObjects){
if (dO.name.equals(name))
drawingObjects.remove(dO);
}
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
drawObjLabels(g);
drawObjWaves(g);
}
private void drawObjLabels(Graphics g){
int index=0;
for (DrawingObjects dO:drawingObjects){
dO.label.setLocation(1, (int)(dO.label.getHeight() * index+1));
System.out.println("Location x y : "+1+" , "+(dO.label.getHeight() * index+1));
System.out.println("Label "+dO.label.getWidth()+" , "+dO.label.getHeight());
System.out.println("Surface Height width "+getSize().getHeight()+" , "+getSize().getWidth());
index++;
}
}
private void drawObjWaves(Graphics g){
}
}
以下是构造它的代码(另一个类中的函数):
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JToolBar;
public class WaveDrawer {
private JFrame mainWindow;
private DrawingSurface drawingSurface;
private boolean drawingState = false;
private boolean editingState = false;
private void setMenuBar(JFrame frame){
JMenuItem screenshot = new JMenuItem("Export as Image");
screenshot.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {}
});
JMenuItem exit = new JMenuItem("Exit");
exit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
frame.dispose();
}
});
JMenu menu = new JMenu("File");
menu.add(screenshot);
menu.add(exit);
JMenuBar mb = new JMenuBar();
mb.add(menu);
frame.setJMenuBar(mb);
}
private void setToolBar(JFrame frame){
JToolBar toolBar = new JToolBar("States");
JButton drawingButton = new JButton();
drawingButton.setText("Drawing");
toolBar.add(drawingButton);
JButton editingButton = new JButton();
editingButton.setText("Editing Circuit");
toolBar.add(editingButton);
Container contentPane = frame.getContentPane();
contentPane.add(toolBar, BorderLayout.NORTH);
}
private void createAndShowGUI() {
// Create and set up the window.
mainWindow = new JFrame("WaveDrawer");
mainWindow.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
mainWindow.setSize(1200, 400);
setMenuBar(mainWindow);
setToolBar(mainWindow);
drawingSurface = new DrawingSurface();
drawingSurface.setLayout(new BorderLayout());
drawingSurface.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.BLUE));
Container contentPane = mainWindow.getContentPane();
contentPane.add(drawingSurface, BorderLayout.CENTER);
mainWindow.setVisible(true);
}
public WaveDrawer() {
createAndShowGUI();
}
/*
* ========== API part
*/
public int registerInput(String name,String comp1,String comp2) {
return drawingSurface.registerInput(name,comp1, comp2);
}
public void unregisterInput(String name) {
drawingSurface.unregisterInput(name);
}
}
这是一个使用main的简单测试:
public class Tester {
public static void main(String[] args) {
WaveDrawer wd = new WaveDrawer();
wd.registerInput("aaa", "aaaa", "aaa");
wd.registerInput("aaa2", "aaaa2", "aaa2");
wd.registerInput("aaa3", "aaaa2", "aaa2");
wd.registerInput("aaa4", "aaaa2", "aaa2");
}
}
这是控制台的输出(我的打印):
Label 119.0 , 56.6
Surface Height width 283.0 , 1190.0
Label 119.0 , 56.6
Surface Height width 283.0 , 1190.0
Location x y : 1 , 1
Label 119 , 56
Surface Height width 283.0 , 1190.0
Location x y : 1 , 57
Label 119 , 56
Surface Height width 283.0 , 1190.0
Location x y : 1 , 1
Label 119 , 56
Surface Height width 313.0 , 1200.0
Location x y : 1 , 312
Label 1198 , 311
Surface Height width 313.0 , 1200.0
注意到第二个标签的位置和大小突然改变了:
Location x y : 1 , 312
Label 1198 , 311
这里有什么问题?