[英]Add JTextField in A new JFrame
I have a JFrame
with a JButton
, this button open a new JFrame
where there should be a text box ( JTextField
) that I will use for a search , the problem is that I don't know how to insert it . 我有一个带JButton
的JFrame
,这个按钮打开一个新的JFrame
,应该在这里有一个我要用于搜索的文本框( JTextField
),问题是我不知道如何插入它。 I came up with this : 我想出了这个:
NB I'm a beginner, sorry in advance for the easy question :) 注意:我是一个初学者,对于这个简单的问题,请您提前表示抱歉:)
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class MainWindow {
// Seconda Finestra
public static void NuovaFinestra (JPanel panel) {
panel.setLayout(null);
JButton Ricerca = new JButton("Ricerca");
Ricerca.setBounds(100, 100, 200, 50);
panel.add(Ricerca);
Ricerca.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
JFrame FinestradiRicerca = new JFrame("Finestra di Ricerca");
FinestradiRicerca.setBounds(300, 300, 500, 500);
FinestradiRicerca.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel riquadroRicerca = new JPanel();
FinestradiRicerca.add(riquadroRicerca);
FinestradiRicerca.setVisible(true);
JTextField ciao;
ciao = new JTextField ();
}
});
}
//Main
public static void main(String[] args) {
//Finestra Principale
JFrame finestra = new JFrame("Finestra principale");
finestra.setSize(500, 500);
finestra.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//JPanel della finestra principale
JPanel riquadro = new JPanel();
finestra.add(riquadro);
finestra.setVisible(true);
NuovaFinestra(riquadro);
}
}
You needed to add your new elements to riquadroRicerca
BEFORE adding the Panel to FinestradiRicerca
, I recommend you NOT to use null
layout but a Layout Manager or combinations of them. 在将面板添加到FinestradiRicerca
之前,需要将新元素添加到riquadroRicerca
,我建议您不要使用null
布局,而是使用布局管理器或它们的组合。 If you insist on keeping null layout then see below example. 如果您坚持保持空布局,请参见以下示例。 But for this kind of app I'd suggest a CardLayout
. 但是对于这种应用程序,我建议使用CardLayout
。
I also suggest not using multiple JFrames
since they will open multiple windows on task bar which is user unfriendly. 我还建议不要使用多个JFrames
因为它们会在任务栏上打开多个窗口,这对用户不友好。 See: Use of multiple JFrames, Good / Bad Practice 请参阅: 多个JFrame的使用,良好/不良实践
As an aside note, follow Java naming conventions . 另外,请遵循Java命名约定 。 For example you called a JFrame
as FinestradiRicerca
instead rename it to: finestradiRicerca
(1st letter of a variable in lowercase). 例如,您将一个JFrame
称为FinestradiRicerca
而不是将其重命名为: finestradiRicerca
( 变量的第一个字母小写)。
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class MainWindow {
// Seconda Finestra
public static void NuovaFinestra (JPanel panel) {
panel.setLayout(null);
JButton Ricerca = new JButton("Ricerca");
Ricerca.setBounds(100, 100, 200, 50);
panel.add(Ricerca);
Ricerca.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
JFrame FinestradiRicerca = new JFrame("Finestra di Ricerca");
FinestradiRicerca.setBounds(300, 300, 500, 500);
//If you don't want to close whole app when closing this windo change it to: JFrame.DISPOSE_ON_CLOSE
FinestradiRicerca.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel riquadroRicerca = new JPanel();
JTextField ciao;
JLabel myLabel = new JLabel("Here goes your label text");
ciao = new JTextField ();
ciao.setColumns(20);
riquadroRicerca.add(myLabel);
riquadroRicerca.add(ciao);
FinestradiRicerca.add(riquadroRicerca);
FinestradiRicerca.setVisible(true);
}
});
}
//Main
public static void main(String[] args) {
//Finestra Principale
JFrame finestra = new JFrame("Finestra principale");
finestra.setSize(500, 500);
finestra.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//JPanel della finestra principale
JPanel riquadro = new JPanel();
finestra.add(riquadro);
finestra.setVisible(true);
NuovaFinestra(riquadro);
}
}
So, your code after a few modifications, to make JLabel
and JTextField
visible gives the following output: 因此,经过一些修改以使JLabel
和JTextField
可见的代码将提供以下输出:
However, please follow my above recomendations. 但是,请遵循我的上述建议。
Add the JTextField
to your new JFrame like this. 像这样将JTextField
添加到新的JFrame中。 You also have to initialize your textfield. 您还必须初始化您的文本字段。 It is basically the same as you have done with the initial JFrame. 基本上与初始JFrame相同。
JTextField ciao = new JTextField();
FinestradiRicerca.add(ciao);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.