我已经(我认为)彻底搜索了我的问题的答案。我是初学者,所以我可能只是不知道要寻找什么。我正在尝试概述办公室布局(桌子、椅子),我使用 Graphics2D 和 GeneralPath 以及每把椅子命名的 JLabels 对其进行编码。如果这已经得到回答,我很抱歉,但我确实看过。(注意:现在的图形非常简单:桌子只是一个正方形,椅子只是线条。)
公共类 DemoReception 扩展 JApplet{
@Override
public void paint(Graphics g){
//draws table
Graphics2D g2 = (Graphics2D) g;
g2.setStroke(new BasicStroke(4.0f));
g2.setColor(Color.BLACK);
int[] xPoints={150,700,700,150};
int[] yPoints={250,250,550,550};
GeneralPath path = new GeneralPath(GeneralPath.WIND_EVEN_ODD,xPoints.length);
path.moveTo(xPoints[0], yPoints[0]);
for (int i = 0; i < xPoints.length; i++) {
path.lineTo(xPoints[i], yPoints[i]);
}
path.closePath();
g2.draw(path);
//draws chairs
g2.setColor(Color.RED);
path = new GeneralPath(GeneralPath.WIND_NON_ZERO);
path.moveTo(260,240);//Person1
path.lineTo(310,240);
path.moveTo(510,240);//Person2
path.lineTo(560,240);
path.moveTo(260,560);//Person3
path.lineTo(310,560);
path.moveTo(510,560);//Person4
path.lineTo(560,560);
path.closePath();
g2.draw(path);
}
这是主要方法:
public static void main(String[] args) {
int labelwidth = 50;
int labelheight = 10;
JFrame testFrame = new JFrame("Test Layout");
testFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JApplet demo = new DemoReception();
testFrame.setBackground(Color.white);
testFrame.getContentPane().add(demo);
testFrame.pack();
testFrame.setMinimumSize(new Dimension(1000,710));
testFrame.setSize(new Dimension(1000,710));
JPanel testPanel = new JPanel();
testPanel.setAlignmentX(0);
testPanel.setAlignmentY(0);
label1 = new JLabel("Person1");
label2 = new JLabel("Person2");
label3 = new JLabel("Person3");
label4 = new JLabel("Person4");
label1.setAlignmentX(260);
label1.setAlignmentY(235);
label1.setSize(labelwidth, labelheight);
label1.setVisible(true);
testPanel.add(label1);
label2.setAlignmentX(510);
label2.setAlignmentY(235);
label2.setSize(labelwidth, labelwidth);
label2.setVisible(true);
testPanel.add(label2);
label3.setAlignmentX(260);
label3.setAlignmentY(565);
label3.setSize(labelwidth, labelwidth);
label3.setVisible(true);
testPanel.add(label3);
label4.setAlignmentX(510);
label4.setAlignmentY(565);
label4.setSize(labelwidth, labelwidth);
label4.setVisible(true);
testPanel.add(label4);
testFrame.getContentPane().add(testPanel);
testFrame.setVisible(true);
}
当我运行它时,我得到的只是带有图形的 JFrame,但 JLabels 没有出现。任何帮助,将不胜感激。