给按钮加事件。 用netbeans做swing比较方便,可以搜一下怎么在netbean里拖拉组件做swing 下面2个类是个例子。Good luck~import javax.swing.JFrame;/* * To change this template, choose Tools | Templates * and open the template in the editor. *//* * MainApp.java * * Created on 15-Oct-2009, 22:31:01 */ /** * * @author Hongyang Sun */ public class MainApp extends javax.swing.JFrame { /** Creates new form XML */ public MainApp() { initComponents(); } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { jComboBox1 = new javax.swing.JComboBox(); jLabel5 = new javax.swing.JLabel(); jButton4 = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("Example MainFrame"); setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR)); jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "SubFrame", "OtherFrame" })); jComboBox1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jComboBox1ActionPerformed(evt); } }); jLabel5.setText("Please choose the Algorithm your data comes from: "); jButton4.setText("OK"); jButton4.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton4ActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(37, 37, 37) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(jComboBox1, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))) .addGroup(layout.createSequentialGroup() .addGap(130, 130, 130) .addComponent(jButton4))) .addContainerGap(39, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addContainerGap(46, Short.MAX_VALUE) .addComponent(jLabel5) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(27, 27, 27) .addComponent(jButton4) .addGap(29, 29, 29)) ); pack(); }// </editor-fold> private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here:
1.定义一个窗口类: class MyFrame extends Frame{ public MyFrame(){ } //定义构造函数 } 2.定义监听事件: public class buttonListener implements ActionListener{ public void actionPerformed(ActionEvent e){ new MyFrame(); } } 3.给Button对象添加监听: myButton.addActionListener(new buttonListener()); 新建窗口对象:
JFrame jf = new JFrame(); jf.setValuable(true);//或jf.show();
这个很难吗?button.addActionListener(new ActionEvent(){ public void performAction(Event e){ NeedShowFrame frame = new NeedShowFrame(); frame.setVisable(true); frame.setSize(100, 100); } });
更正下: button.addActionListener(new ActionListener(){ public void performAction(ActionEvent e){ NeedShowFrame frame = new NeedShowFrame(); frame.setVisable(true); frame.setSize(100, 100); } });
frm.show()
你试试吧,都忘了
用netbeans做swing比较方便,可以搜一下怎么在netbean里拖拉组件做swing
下面2个类是个例子。Good luck~import javax.swing.JFrame;/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*//*
* MainApp.java
*
* Created on 15-Oct-2009, 22:31:01
*/
/**
*
* @author Hongyang Sun
*/
public class MainApp extends javax.swing.JFrame { /** Creates new form XML */
public MainApp() {
initComponents();
} /** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() { jComboBox1 = new javax.swing.JComboBox();
jLabel5 = new javax.swing.JLabel();
jButton4 = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Example MainFrame");
setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR)); jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "SubFrame", "OtherFrame" }));
jComboBox1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jComboBox1ActionPerformed(evt);
}
}); jLabel5.setText("Please choose the Algorithm your data comes from: "); jButton4.setText("OK");
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton4ActionPerformed(evt);
}
}); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(37, 37, 37)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jComboBox1, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
.addGroup(layout.createSequentialGroup()
.addGap(130, 130, 130)
.addComponent(jButton4)))
.addContainerGap(39, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(46, Short.MAX_VALUE)
.addComponent(jLabel5)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(27, 27, 27)
.addComponent(jButton4)
.addGap(29, 29, 29))
); pack();
}// </editor-fold> private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String algo = jComboBox1.getSelectedItem().toString();
if(algo.equals("SubFrame")){
JFrame j = new SubFrame();
j.setLocationByPlatform(true);
j.setLocationRelativeTo(null);
j.setVisible(true);
}
javax.swing.JButton jbuttonx = new javax.swing.JButton();
jbuttonx.setText("test");
jbuttonx.setName("jButtonx"); add(jbuttonx);
} private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {
} public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() { public void run() {
new MainApp().setVisible(true);
}
});
}
private javax.swing.JButton jButton4;
private javax.swing.JComboBox jComboBox1;
private javax.swing.JLabel jLabel5;
}import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;public class SubFrame extends javax.swing.JFrame {
static boolean loadMotifFile = false;
static boolean loadRegulatorFile = false;
static boolean loadGoFile = false;
boolean loadCondAnnotationCategoryFile = false;
boolean showFinishPop = false;
public SubFrame() {
initComponents();
} @SuppressWarnings("unchecked")
private void initComponents() { jLabel3 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jLabel4 = new javax.swing.JLabel(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Example"); jLabel3.setText("Output Data:"); jTextField1.setText("Necessary");
jTextField1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField1ActionPerformed(evt);
}
}); jButton1.setText("Browse");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
}); jLabel4.setText("Required Data"); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(43, 43, 43)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel4)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 529, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 66, Short.MAX_VALUE)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 257, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(124, 124, 124)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(52, 52, 52))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(27, 27, 27)
.addComponent(jLabel4)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton1))
.addGap(34, 34, 34))
); pack();
} private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
} private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
File f = null;
String fileName = "";
try {
f = new File(fileName);
} catch (Exception ex) {
f = null;
} JFileChooser fileChooser = new JFileChooser(); fileChooser.setDialogTitle("Select a file to open...");
fileChooser.setFileHidingEnabled(false);
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
fileChooser.setSelectedFile(f); int choice = fileChooser.showOpenDialog(this);
if (choice == JFileChooser.APPROVE_OPTION) {
fileName = fileChooser.getSelectedFile().getAbsolutePath();
System.out.println("Starting with file: " + fileName); jTextField1.setText(fileName);
}
} public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() { public void run() {
new SubFrame().setVisible(true);
}
});
}
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JTextField jTextField1;
}
class MyFrame extends Frame{
public MyFrame(){
} //定义构造函数
}
2.定义监听事件:
public class buttonListener implements ActionListener{
public void actionPerformed(ActionEvent e){
new MyFrame();
}
}
3.给Button对象添加监听:
myButton.addActionListener(new buttonListener());
新建窗口对象:
jf.setValuable(true);//或jf.show();
public void performAction(Event e){
NeedShowFrame frame = new NeedShowFrame();
frame.setVisable(true);
frame.setSize(100, 100);
}
});
更正下:
button.addActionListener(new ActionListener(){
public void performAction(ActionEvent e){
NeedShowFrame frame = new NeedShowFrame();
frame.setVisable(true);
frame.setSize(100, 100);
}
});
form f=new Form();
f.show()
前一个Frame setvisible(false)