package ygh;import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JToolBar;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import java.awt.BorderLayout;
import javax.swing.BorderFactory;
import java.awt.Rectangle;
import java.awt.Font;
import javax.swing.*;
import java.awt.Graphics;
import java.applet.Applet;/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2006</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class helloworldFrame extends JFrame {
JPanel contentPane;
BorderLayout borderLayout1 = new BorderLayout();
JMenuBar jMenuBar1 = new JMenuBar();
JMenu jMenuFile = new JMenu();
JMenuItem jMenuFileExit = new JMenuItem();
JMenu jMenuHelp = new JMenu();
JMenuItem jMenuHelpAbout = new JMenuItem();
JToolBar jToolBar = new JToolBar();
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
JButton jButton3 = new JButton();
ImageIcon image1 = new ImageIcon(ygh.helloworldFrame.class.getResource(
"openFile.png"));
ImageIcon image2 = new ImageIcon(ygh.helloworldFrame.class.getResource(
"closeFile.png"));
ImageIcon image3 = new ImageIcon(ygh.helloworldFrame.class.getResource(
"help.png"));
JLabel statusBar = new JLabel();
JPanel jPanel1 = new JPanel();
JLabel jLabel1 = new JLabel();
JMenu jMenu1 = new JMenu();
JMenuItem jMenuItem1 = new JMenuItem();
private int i=0; public helloworldFrame() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
private void jbInit() throws Exception {
contentPane = (JPanel) getContentPane();
contentPane.setLayout(borderLayout1);
setSize(new Dimension(400, 300));
setTitle("helloworld");
statusBar.setText(" ");
jMenuFile.setText("File");
jMenuFileExit.setText("Exit");
jMenuFileExit.addActionListener(new
helloworldFrame_jMenuFileExit_ActionAdapter(this));
jMenuHelp.setText("Help");
jMenuHelpAbout.setText("About");
jMenuHelpAbout.addActionListener(new
helloworldFrame_jMenuHelpAbout_ActionAdapter(this));
jPanel1.setBackground(Color.white);
jPanel1.setBorder(BorderFactory.createEtchedBorder());
jPanel1.setLayout(null);
jLabel1.setFont(new java.awt.Font("Serif", Font.PLAIN, 28));
jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
jLabel1.setText("hellow world!");
jLabel1.setBounds(new Rectangle(132, 111, 218, 74));
jMenuItem1.setText("pain");
jMenuItem1.addActionListener(new
helloworldFrame_jMenuItem1_actionAdapter(this));
jMenuBar1.add(jMenuFile);
jMenuFile.add(jMenuFileExit);
jMenuFile.add(jMenuItem1);
jMenuBar1.add(jMenuHelp);
jMenuHelp.add(jMenuHelpAbout);
setJMenuBar(jMenuBar1);
jButton1.setIcon(image1);
jButton1.setToolTipText("Open File");
jButton2.setIcon(image2);
jButton2.setToolTipText("Close File");
jButton3.setIcon(image3);
jButton3.setToolTipText("Help");
jToolBar.add(jButton1);
jToolBar.add(jButton2);
jToolBar.add(jButton3);
contentPane.add(jToolBar, BorderLayout.NORTH);
contentPane.add(statusBar, BorderLayout.SOUTH);
contentPane.add(jPanel1, java.awt.BorderLayout.CENTER);
jPanel1.add(jLabel1);
} void jMenuFileExit_actionPerformed(ActionEvent actionEvent) {
System.exit(0);
}
void jMenuHelpAbout_actionPerformed(ActionEvent actionEvent) {
helloworldFrame_AboutBox dlg = new helloworldFrame_AboutBox(this);
Dimension dlgSize = dlg.getPreferredSize();
Dimension frmSize = getSize();
Point loc = getLocation();
dlg.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x,
(frmSize.height - dlgSize.height) / 2 + loc.y);
dlg.setModal(true);
dlg.pack();
dlg.setVisible(true);
} public void jMenuItem1_actionPerformed(ActionEvent e) {
}
}
class helloworldFrame_jMenuFileExit_ActionAdapter implements ActionListener {
helloworldFrame adaptee; helloworldFrame_jMenuFileExit_ActionAdapter(helloworldFrame adaptee) {
this.adaptee = adaptee;
} public void actionPerformed(ActionEvent actionEvent) {
adaptee.jMenuFileExit_actionPerformed(actionEvent);
}
}
class helloworldFrame_jMenuItem1_actionAdapter implements ActionListener {
private helloworldFrame adaptee;
helloworldFrame_jMenuItem1_actionAdapter(helloworldFrame adaptee) {
this.adaptee = adaptee;
} public void actionPerformed(ActionEvent e) {
adaptee.jMenuItem1_actionPerformed(e);
}
}
class helloworldFrame_jMenuHelpAbout_ActionAdapter implements ActionListener {
helloworldFrame adaptee; helloworldFrame_jMenuHelpAbout_ActionAdapter(helloworldFrame adaptee) {
this.adaptee = adaptee;
} public void actionPerformed(ActionEvent actionEvent) {
adaptee.jMenuHelpAbout_actionPerformed(actionEvent);
}
}
import java.awt.event.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JToolBar;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import java.awt.BorderLayout;
import javax.swing.BorderFactory;
import java.awt.Rectangle;
import java.awt.Font;
import javax.swing.*;
import java.awt.Graphics;
import java.applet.Applet;/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2006</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class helloworldFrame extends JFrame {
JPanel contentPane;
BorderLayout borderLayout1 = new BorderLayout();
JMenuBar jMenuBar1 = new JMenuBar();
JMenu jMenuFile = new JMenu();
JMenuItem jMenuFileExit = new JMenuItem();
JMenu jMenuHelp = new JMenu();
JMenuItem jMenuHelpAbout = new JMenuItem();
JToolBar jToolBar = new JToolBar();
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
JButton jButton3 = new JButton();
ImageIcon image1 = new ImageIcon(ygh.helloworldFrame.class.getResource(
"openFile.png"));
ImageIcon image2 = new ImageIcon(ygh.helloworldFrame.class.getResource(
"closeFile.png"));
ImageIcon image3 = new ImageIcon(ygh.helloworldFrame.class.getResource(
"help.png"));
JLabel statusBar = new JLabel();
JPanel jPanel1 = new JPanel();
JLabel jLabel1 = new JLabel();
JMenu jMenu1 = new JMenu();
JMenuItem jMenuItem1 = new JMenuItem();
private int i=0; public helloworldFrame() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
private void jbInit() throws Exception {
contentPane = (JPanel) getContentPane();
contentPane.setLayout(borderLayout1);
setSize(new Dimension(400, 300));
setTitle("helloworld");
statusBar.setText(" ");
jMenuFile.setText("File");
jMenuFileExit.setText("Exit");
jMenuFileExit.addActionListener(new
helloworldFrame_jMenuFileExit_ActionAdapter(this));
jMenuHelp.setText("Help");
jMenuHelpAbout.setText("About");
jMenuHelpAbout.addActionListener(new
helloworldFrame_jMenuHelpAbout_ActionAdapter(this));
jPanel1.setBackground(Color.white);
jPanel1.setBorder(BorderFactory.createEtchedBorder());
jPanel1.setLayout(null);
jLabel1.setFont(new java.awt.Font("Serif", Font.PLAIN, 28));
jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
jLabel1.setText("hellow world!");
jLabel1.setBounds(new Rectangle(132, 111, 218, 74));
jMenuItem1.setText("pain");
jMenuItem1.addActionListener(new
helloworldFrame_jMenuItem1_actionAdapter(this));
jMenuBar1.add(jMenuFile);
jMenuFile.add(jMenuFileExit);
jMenuFile.add(jMenuItem1);
jMenuBar1.add(jMenuHelp);
jMenuHelp.add(jMenuHelpAbout);
setJMenuBar(jMenuBar1);
jButton1.setIcon(image1);
jButton1.setToolTipText("Open File");
jButton2.setIcon(image2);
jButton2.setToolTipText("Close File");
jButton3.setIcon(image3);
jButton3.setToolTipText("Help");
jToolBar.add(jButton1);
jToolBar.add(jButton2);
jToolBar.add(jButton3);
contentPane.add(jToolBar, BorderLayout.NORTH);
contentPane.add(statusBar, BorderLayout.SOUTH);
contentPane.add(jPanel1, java.awt.BorderLayout.CENTER);
jPanel1.add(jLabel1);
} void jMenuFileExit_actionPerformed(ActionEvent actionEvent) {
System.exit(0);
}
void jMenuHelpAbout_actionPerformed(ActionEvent actionEvent) {
helloworldFrame_AboutBox dlg = new helloworldFrame_AboutBox(this);
Dimension dlgSize = dlg.getPreferredSize();
Dimension frmSize = getSize();
Point loc = getLocation();
dlg.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x,
(frmSize.height - dlgSize.height) / 2 + loc.y);
dlg.setModal(true);
dlg.pack();
dlg.setVisible(true);
} public void jMenuItem1_actionPerformed(ActionEvent e) {
}
}
class helloworldFrame_jMenuFileExit_ActionAdapter implements ActionListener {
helloworldFrame adaptee; helloworldFrame_jMenuFileExit_ActionAdapter(helloworldFrame adaptee) {
this.adaptee = adaptee;
} public void actionPerformed(ActionEvent actionEvent) {
adaptee.jMenuFileExit_actionPerformed(actionEvent);
}
}
class helloworldFrame_jMenuItem1_actionAdapter implements ActionListener {
private helloworldFrame adaptee;
helloworldFrame_jMenuItem1_actionAdapter(helloworldFrame adaptee) {
this.adaptee = adaptee;
} public void actionPerformed(ActionEvent e) {
adaptee.jMenuItem1_actionPerformed(e);
}
}
class helloworldFrame_jMenuHelpAbout_ActionAdapter implements ActionListener {
helloworldFrame adaptee; helloworldFrame_jMenuHelpAbout_ActionAdapter(helloworldFrame adaptee) {
this.adaptee = adaptee;
} public void actionPerformed(ActionEvent actionEvent) {
adaptee.jMenuHelpAbout_actionPerformed(actionEvent);
}
}
你可以用Line2D试试看
就是先new一个实例
然后修改它的属性好了
g.drawLine(...)方法也可以