如何将java中用虚线连起来各个层次内容

 我来答
超人年0v
2017-07-23 · TA获得超过237个赞
知道小有建树答主
回答量:408
采纳率:0%
帮助的人:151万
展开全部
<pre t="code" l="cpp">package bdzhidao;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class DrawingBoard extends JFrame{
int[] xPoint={200,300,500};
int[] yPoint={100,180,240};
static int m=15;
static Color color=new Color(255,0,0);
private JComboBox jcb2=new JComboBox(new Object[]{
" 20"," 28"," 36"," 44"," 52"," 72 "
});

private JComboBox jcb3=new JComboBox(new Object[]{
"红色","黑色","蓝色","绿色"
});
public DrawingBoard(){
JMenuBar Bar=new JMenuBar();
JMenu jmFile=new JMenu("文件");
JMenuItem jmi1=new JMenuItem("新建");
JMenuItem jmi2=new JMenuItem("打开");
JMenuItem jmi3=new JMenuItem("保存");
JMenuItem jmi4=new JMenuItem("退出");
jmFile.add(jmi1);
jmFile.add(jmi2);
jmFile.add(jmi3);
jmFile.add(jmi4);
JLabel jlb1=new JLabel("线条");
JLabel jlb2=new JLabel("线宽");
JLabel jlb3=new JLabel("颜色");
final JComboBox jcb1=new JComboBox(new Object[]{
"实线","虚线"
});

JButton jbt0=new JButton("直线");
JButton jbt1=new JButton("三角形");
JButton jbt2=new JButton("圆");
JButton jbt3=new JButton("矩形");

JPanel jp1=new JPanel();
jp1.add(jlb1);
jp1.add(jcb1);
jp1.add(jlb2);
jp1.add(jcb2);
jp1.add(jlb3);
jp1.add(jcb3);
jp1.add(jbt0);
jp1.add(jbt1);
jp1.add(jbt2);
jp1.add(jbt3);

final DrawingPanel jp2=new DrawingPanel();
jp2.setBackground(Color.white);
jp2.setPreferredSize(new Dimension(736,300));

this.add(Bar);
this.add(jp1,BorderLayout.CENTER);
this.add(jp2,BorderLayout.SOUTH);

jcb1.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent e){
if(jcb1.getSelectedItem()=="实线"){
Graphics g=jp2.getGraphics();
g.drawLine(10,10,736,10);
}
if(jcb1.getSelectedItem()=="虚线"){
Graphics g=jp2.getGraphics();
for(int i=1;i<100;i++){
g.drawLine(6*i-6,50,6*i-3,50);
}
}
}
});

jcb2.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent e){
if(jcb2.getSelectedItem()==" 20"){
m=20;
repaint();
}
if(jcb2.getSelectedItem()==" 28"){
m=28;repaint();
}
if(jcb2.getSelectedItem()==" 36"){
m=36;repaint();
}
if(jcb2.getSelectedItem()==" 44"){
m=44;repaint();
}
if(jcb2.getSelectedItem()==" 52"){
m=52;repaint();
}
if(jcb2.getSelectedItem()==" 72 "){
m=72;repaint();
}
}
});

jcb3.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent e){
if(jcb3.getSelectedItem()=="红色"){
color=new Color(255,0,0);
repaint();
}
if(jcb3.getSelectedItem()=="蓝色"){
color=new Color(0,0,255);
repaint();
}
if(jcb3.getSelectedItem()=="黑色"){
color=new Color(0,0,0);
repaint();
}
if(jcb3.getSelectedItem()=="绿色"){
color=new Color(0,128,0);
repaint();
}
}
});
jbt0.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
Graphics g=jp2.getGraphics();
g.drawLine(10,10,736,10);
}
});

jbt1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
Graphics grap=jp2.getGraphics();//创建所画组件对象
grap.drawPolygon(xPoint,yPoint,3);
}
});
jbt2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
Graphics gr=jp2.getGraphics();
gr.drawOval(100,10,200,200);
}
});
jbt3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
Graphics G= jp2.getGraphics();
G.drawRect(500,50,200,200);
}
});

}
public static void main(String[] args){
JFrame frame=new DrawingBoard();
frame.setTitle("测试画图板");
frame.setSize(736,379);
frame.setLocation(100,100);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
static class DrawingPanel extends JPanel{
protected void paintComponent(Graphics g){
super.paintComponent(g);
g.setFont(new Font("宋体",Font.BOLD,m));
g.setColor(color);
g.drawString("Heleoeo ",100, 100);
}
}

}
有一些问题还没有解决,因为重画,所以字符串可以重画,别的我没有重画,你自己可以修改一下,
还有一个问题,你的菜单条上的功能我暂时不会写,所以没有加上去,
希望对你有帮助,一起努力吧!!!
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式