Java 关于getText()取不到值的问题
以下是代码,主要问题是当点击“新建”弹出dialog后,在NameFrame里输了string,可是为什么getText()取不到呢,internalframe的名称总是...
以下是代码,主要问题是当点击“新建”弹出dialog后,在NameFrame里输了string,可是为什么getText()取不到呢,internalframe的名称总是空的,是不是顺序错了还是少什么?应该怎么改,各位大神帮帮忙啊~~
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Test extends JFrame {
JDesktopPane desktoppane = new JDesktopPane();
String Name;
public Test(){
Container contentPane = this.getContentPane();
contentPane.add(desktoppane);
creatMenu();
setTitle("Test");
setSize(500, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
//创建菜单栏
public void creatMenu(){
JMenuBar MyJMenuBar = new JMenuBar();
JMenu file = new JMenu("文件");
MyJMenuBar.add(file);
JMenuItem build = new JMenuItem("新建");
file.add(build);
this.setJMenuBar(MyJMenuBar);
build.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
creatDialog();
}
});
}
//创建对话框
public void creatDialog(){
final JDialog dialog = new JDialog(this,"对话框");
dialog.setLayout(new GridBagLayout());
JLabel blank1 = new JLabel(" ");
JLabel name = new JLabel("名称:");
JTextField NameFrame = new JTextField(10);
JButton button1 = new JButton("提交");
GridBagConstraints c = new GridBagConstraints();
dialog.getContentPane().add(name,c);
dialog.getContentPane().add(NameFrame,c);
c.gridy=1;
dialog.getContentPane().add(button1,c);
Name = NameFrame.getText().trim();
// System.out.println(Name);
NameFrame.setText("");
dialog.setSize(300,350);
dialog.setLocation(550,150);
dialog.setVisible(true);
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
creatInternalframe();
dialog.dispose();
}
});
}
//创建内部窗口
public void creatInternalframe(){
JInternalFrame internalframe = new JInternalFrame(Name,true,true,true,true);
// internalframe.setTitle(Name);
internalframe.setSize(400,400);
internalframe.setVisible(true);
Container icontentPane = internalframe.getContentPane();
icontentPane.add(new JEditorPane());
desktoppane.add(internalframe);
}
public static void main(String[] args) {
Test designer = new Test();
}
} 展开
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Test extends JFrame {
JDesktopPane desktoppane = new JDesktopPane();
String Name;
public Test(){
Container contentPane = this.getContentPane();
contentPane.add(desktoppane);
creatMenu();
setTitle("Test");
setSize(500, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
//创建菜单栏
public void creatMenu(){
JMenuBar MyJMenuBar = new JMenuBar();
JMenu file = new JMenu("文件");
MyJMenuBar.add(file);
JMenuItem build = new JMenuItem("新建");
file.add(build);
this.setJMenuBar(MyJMenuBar);
build.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
creatDialog();
}
});
}
//创建对话框
public void creatDialog(){
final JDialog dialog = new JDialog(this,"对话框");
dialog.setLayout(new GridBagLayout());
JLabel blank1 = new JLabel(" ");
JLabel name = new JLabel("名称:");
JTextField NameFrame = new JTextField(10);
JButton button1 = new JButton("提交");
GridBagConstraints c = new GridBagConstraints();
dialog.getContentPane().add(name,c);
dialog.getContentPane().add(NameFrame,c);
c.gridy=1;
dialog.getContentPane().add(button1,c);
Name = NameFrame.getText().trim();
// System.out.println(Name);
NameFrame.setText("");
dialog.setSize(300,350);
dialog.setLocation(550,150);
dialog.setVisible(true);
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
creatInternalframe();
dialog.dispose();
}
});
}
//创建内部窗口
public void creatInternalframe(){
JInternalFrame internalframe = new JInternalFrame(Name,true,true,true,true);
// internalframe.setTitle(Name);
internalframe.setSize(400,400);
internalframe.setVisible(true);
Container icontentPane = internalframe.getContentPane();
icontentPane.add(new JEditorPane());
desktoppane.add(internalframe);
}
public static void main(String[] args) {
Test designer = new Test();
}
} 展开
展开全部
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Test extends JFrame {
JDesktopPane desktoppane = new JDesktopPane();
String Name;
public Test(){
Container contentPane = this.getContentPane();
contentPane.add(desktoppane);
creatMenu();
setTitle("Test");
setSize(500, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
//创建菜单栏
public void creatMenu(){
JMenuBar MyJMenuBar = new JMenuBar();
JMenu file = new JMenu("文件");
MyJMenuBar.add(file);
JMenuItem build = new JMenuItem("新建");
file.add(build);
this.setJMenuBar(MyJMenuBar);
build.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
creatDialog();
}
});
}
//创建对话框
public void creatDialog(){
final JDialog dialog = new JDialog(this,"对话框");
dialog.setLayout(new GridBagLayout());
JLabel blank1 = new JLabel(" ");
JLabel name = new JLabel("名称:");
final JTextField NameFrame = new JTextField(10);
JButton button1 = new JButton("提交");
GridBagConstraints c = new GridBagConstraints();
dialog.getContentPane().add(name,c);
dialog.getContentPane().add(NameFrame,c);
c.gridy=1;
dialog.getContentPane().add(button1,c);
NameFrame.setText("");
dialog.setSize(300,350);
dialog.setLocation(550,150);
dialog.setVisible(true);
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
Name = NameFrame.getText().trim();
System.out.println(Name);
creatInternalframe();
dialog.dispose();
}
});
}
//创建内部窗口
public void creatInternalframe(){
JInternalFrame internalframe = new JInternalFrame(Name,true,true,true,true);
// internalframe.setTitle(Name);
internalframe.setSize(400,400);
internalframe.setVisible(true);
Container icontentPane = internalframe.getContentPane();
icontentPane.add(new JEditorPane());
desktoppane.add(internalframe);
}
public static void main(String[] args) {
Test designer = new Test();
}
}
帮你调好了,你这是逻辑错误,getText()是要在Button触发监听之后执行才是有效的,希望及时采纳
import java.awt.*;
import java.awt.event.*;
public class Test extends JFrame {
JDesktopPane desktoppane = new JDesktopPane();
String Name;
public Test(){
Container contentPane = this.getContentPane();
contentPane.add(desktoppane);
creatMenu();
setTitle("Test");
setSize(500, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
//创建菜单栏
public void creatMenu(){
JMenuBar MyJMenuBar = new JMenuBar();
JMenu file = new JMenu("文件");
MyJMenuBar.add(file);
JMenuItem build = new JMenuItem("新建");
file.add(build);
this.setJMenuBar(MyJMenuBar);
build.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
creatDialog();
}
});
}
//创建对话框
public void creatDialog(){
final JDialog dialog = new JDialog(this,"对话框");
dialog.setLayout(new GridBagLayout());
JLabel blank1 = new JLabel(" ");
JLabel name = new JLabel("名称:");
final JTextField NameFrame = new JTextField(10);
JButton button1 = new JButton("提交");
GridBagConstraints c = new GridBagConstraints();
dialog.getContentPane().add(name,c);
dialog.getContentPane().add(NameFrame,c);
c.gridy=1;
dialog.getContentPane().add(button1,c);
NameFrame.setText("");
dialog.setSize(300,350);
dialog.setLocation(550,150);
dialog.setVisible(true);
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
Name = NameFrame.getText().trim();
System.out.println(Name);
creatInternalframe();
dialog.dispose();
}
});
}
//创建内部窗口
public void creatInternalframe(){
JInternalFrame internalframe = new JInternalFrame(Name,true,true,true,true);
// internalframe.setTitle(Name);
internalframe.setSize(400,400);
internalframe.setVisible(true);
Container icontentPane = internalframe.getContentPane();
icontentPane.add(new JEditorPane());
desktoppane.add(internalframe);
}
public static void main(String[] args) {
Test designer = new Test();
}
}
帮你调好了,你这是逻辑错误,getText()是要在Button触发监听之后执行才是有效的,希望及时采纳
追问
谢谢你~~我把你的复制过去以后NameFrame有问题,这是为什么,然后只能把NameFrame设成public,能出结果,但是显示有错误。。您好人做到底啊。。
2012-03-29
展开全部
你试一下:Name = dialog.NameFrame.getText().trim();
要不然就把dialog 写成内部类,把NameFrame放进去再取。
要不然就把dialog 写成内部类,把NameFrame放进去再取。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询