单击jframe中的jbutton,我想要执行Start.java文件;该怎么办? Start.java执行得很好。我希望在jframe中单击jbutton时执行相同的执行,请帮助我。
我的jframe代码有jbutton
private void jButton3MouseClicked(java.awt.event.MouseEvent evt) {
// i want to execute Start.java file on clicking the button
Start s = new Start();
String in = s.getTxt;
System.out.println(in);
repaint();
}
我的Start.java文件是:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Start extends JPanel{
public String getTxt;
public Start()
{
final JTextField jf = new JTextField(20);
jf.setBounds(30, 30, 250, 30);
final JButton j1 = new JButton("OK");
j1.setBounds(80, 80, 100, 30);
setLayout(null);
add(jf);
add(j1);
j1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
getTxt = jf.getText();
System.out.println(getTxt);
}
});
}
public static void main(String[] args) {
JFrame f = new JFrame("Interest");
f.getContentPane().add(new Start());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setPreferredSize(new Dimension(310,250));
f.setMaximumSize(new Dimension(310,250));
f.setMinimumSize(new Dimension(310,250));
f.pack();
f.setVisible(true);
}
}
答案 0 :(得分:4)
所以,基于你在代码中所做的事情,我强烈建议你看一下How to Make Dialogs,主要原因是,模态对话框会导致你的程序执行停止在对话框可见的位置,并在关闭时恢复,这样,您可以检查用户可能已更改的值。
所以,当我第一次尝试让你的程序运行时,就会发生这种情况......
因此,在深入研究代码后,我注意到您已完成setLayout(null);
。这将继续困扰你,我强烈建议你不要这样做并学习如何使用布局管理API。
所以,我跳进你的代码并添加了一个布局管理器......
public class Start extends JPanel {
public String getTxt;
public Start() {
final JTextField jf = new JTextField(20);
final JButton j1 = new JButton("OK");
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
add(jf, gbc);
add(j1, gbc);
j1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
getTxt = jf.getText();
System.out.println(getTxt);
}
});
}
public static void main(String[] args) {
JFrame f = new JFrame("Interest");
f.getContentPane().add(new Start());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setPreferredSize(new Dimension(310, 250));
f.setMaximumSize(new Dimension(310, 250));
f.setMinimumSize(new Dimension(310, 250));
f.pack();
f.setVisible(true);
}
}
现在我明白了......
好的,但是现在有两个按钮,除非用户点击中间按钮,否则文本永远不会被设置!
事实是,对于这种事情,你不需要按钮!您只需要一个可以返回JTextField
的当前文本的方法,例如......
public class Start extends JPanel {
final JTextField jf = new JTextField(20);
public Start() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
add(jf, gbc);
}
public String getText() {
return jf.getText();
}
public static void main(String[] args) {
JFrame f = new JFrame("Interest");
f.getContentPane().add(new Start());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setPreferredSize(new Dimension(310, 250));
f.setMaximumSize(new Dimension(310, 250));
f.setMinimumSize(new Dimension(310, 250));
f.pack();
f.setVisible(true);
}
}
然后我可以使用......
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setLayout(new GridBagLayout());
JButton btn = new JButton("Surprise");
add(btn);
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Start start = new Start();
JOptionPane.showMessageDialog(TestPane.this, start, "Surprise", JOptionPane.PLAIN_MESSAGE);
System.out.println(start.getText());
}
});
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
}
}
显示它!
我强烈建议您花时间查看Laying Out Components Within a Container