[英]How do you fit all elements in JPanel?
I am having two list and three buttons . 我有两个列表和三个按钮。 The list has elements which are basic shapes. 该列表具有基本形状的元素。 When I select for ex "Rect" it should draw rectangle in the canvas . 当我选择ex“ Rect”时,应该在画布上绘制矩形。 Right now I have implemented for the second list Rectangle. 现在,我已经为第二个列表Rectangle实现了。 The rectangle is drawn perfectly but my lists are getting duplicated again why ?? 矩形绘制得很完美,但是我的列表又被重复了,为什么? And how to fit them nicely ? 以及如何很好地适应它们? . 。 Where am I doing wrong 我在哪里做错了
this is the following code : 这是下面的代码:
package src;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Rectangle2D;
public class Main implements ActionListener{
public static void main(String[] args){
Main gui = new Main();
gui.go();
}
public void go(){
frame = new JFrame();
panel = new JPanel();
String figures[] = {"Rectangle","Rounded Rectangle", "Arc", "Line","Cubic curve"};
drawing1 = new JList<String>(figures);
drawing2 = new JList<String>(figures);
connect_button = new JButton("connect them");
submit1 = new JButton("Submit figure 1");
submit2 = new JButton("Submit figure 2");
connect_button.addActionListener(this);
submit1.addActionListener(this);
submit2.addActionListener(this);
panel.add(drawing2);
panel.add(drawing1);
panel.add(connect_button);
panel.add(submit1);
panel.add(submit2);
panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));
panel.setBackground(Color.gray);
frame.getContentPane().add(BorderLayout.NORTH,panel);
frame.getContentPane().add(BorderLayout.CENTER,draw);
frame.setSize(5000,400);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent event){
if(event.getSource()== submit1){
}
else if(event.getSource()== submit2){
type = drawing1.getSelectedValue();
draw.repaint();
}
else if(event.getSource()== connect_button){
}
}
DrawingPanel draw = new DrawingPanel();
JPanel panel;
JFrame frame;
JButton connect_button;
JButton submit1;
JButton submit2;
JList<String> drawing1;
JList<String> drawing2;
String type;
public class DrawingPanel extends JPanel{
public void paintComponent(Graphics G){
Graphics2D g2d = (Graphics2D) G;
if(type=="Rectangle"){
Rectangle2D r2d = new Rectangle2D.Float(10f, 10f, 130f, 130f);
g2d.draw(r2d);
}
}
}
}
Before clicking on submit figure 2 http://img152.imageshack.us/img152/4594/capture1ypd.png 点击提交之前,请单击图2 http://img152.imageshack.us/img152/4594/capture1ypd.png
After clicking on the submit button http://img46.imageshack.us/img46/854/capture2bk.png 单击提交按钮后http://img46.imageshack.us/img46/854/capture2bk.png
Your list is getting duplicate because you yourself are creating two similar lists 您的列表正在重复,因为您自己正在创建两个类似的列表
String figures[] = {"Rectangle","Rounded Rectangle", "Arc", "Line","Cubic curve"};
drawing1 = new JList<String>(figures);
drawing2 = new JList<String>(figures);
Here both the lists are passed the same figures
, so definitely they will be duplicate. 在这里,两个列表都传递相同的figures
,因此肯定会重复。 Try to give different String[]
to each of them 尝试给每个String[]
赋予不同的String[]
hope this helps! 希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.