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=1i<100i++){
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=28repaint()
}
if(jcb2.getSelectedItem()==" 36"){
m=36repaint()
}
if(jcb2.getSelectedItem()==" 44"){
m=44repaint()
}
if(jcb2.getSelectedItem()==" 52"){
m=52repaint()
}
if(jcb2.getSelectedItem()==" 72 "){
m=72repaint()
}
}
})
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)
}
}
}
按照你的要求编写的JavaGUI程序如下:
import java.awt.GridLayoutimport java.awt.event.ActionEvent
import java.awt.event.ActionListener
import javax.swing.JButton
import javax.swing.JFrame
import javax.swing.JPanel
import javax.swing.JTextField
public class HH extends JFrame implements ActionListener{
JPanel jp1=new JPanel()
JPanel jp2=new JPanel()
JPanel jp3=new JPanel()
JTextField jtf=new JTextField(20)
JButton jb1=new JButton("显示")
JButton jb2=new JButton("清除")
HH(){
jb1.addActionListener(this)
jb2.addActionListener(this)
jp1.setLayout(new GridLayout(2,1))
jp3.add(jtf)
jp2.add(jb1)jp2.add(jb2)
jp1.add(jp3)jp1.add(jp2)
getContentPane().add(jp1)
setSize(300, 120)
setLocationRelativeTo(null)
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
setVisible(true)
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==jb1){
jtf.setText("java程序设计")
}
if(e.getSource()==jb2){
jtf.setText("")
}
}
public static void main(String[] args) {
new HH()
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)