有关java GUI中的显示问题。
我写了一个windows窗体,其中有一个Button和TextFiled。我定义了一个ClassTry类,类中有一个方法addText(),想通过调用该方法将某个Stri...
我写了一个windows窗体,其中有一个Button 和TextFiled。
我定义了一个ClassTry类,类中有一个方法addText(), 想通过调用该方法将某个String写入到窗口的TextField中,请问该如何实现?代码如下:
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextField;
public class InterFace {
private JFrame frame;
private JButton btnNewButton;
private JTextField textField;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
InterFace window = new InterFace();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public InterFace() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 617, 462);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
btnNewButton = new JButton("New button");
btnNewButton.setBounds(105, 193, 110, 49);
frame.getContentPane().add(btnNewButton);
textField = new JTextField();
textField.setBounds(224, 288, 66, 21);
frame.getContentPane().add(textField);
textField.setColumns(10);
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ClassTry CT=new ClassTry();
CT.addText();
}
});
}
}
public class ClassTry {
public void addText()
{
System.out.print("想输出到窗体中 的textField 中!!!");
}
} 展开
我定义了一个ClassTry类,类中有一个方法addText(), 想通过调用该方法将某个String写入到窗口的TextField中,请问该如何实现?代码如下:
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextField;
public class InterFace {
private JFrame frame;
private JButton btnNewButton;
private JTextField textField;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
InterFace window = new InterFace();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public InterFace() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 617, 462);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
btnNewButton = new JButton("New button");
btnNewButton.setBounds(105, 193, 110, 49);
frame.getContentPane().add(btnNewButton);
textField = new JTextField();
textField.setBounds(224, 288, 66, 21);
frame.getContentPane().add(textField);
textField.setColumns(10);
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ClassTry CT=new ClassTry();
CT.addText();
}
});
}
}
public class ClassTry {
public void addText()
{
System.out.print("想输出到窗体中 的textField 中!!!");
}
} 展开
2011-07-21
展开全部
public class MyDesktopPane extends JFrame implements ActionListener {
final static JDesktopPane desktopPane = new JDesktopPane();
public MyDesktopPane() {
super("MyDesktopPane.java:DesktopPane测试");
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("新增窗口");
JMenuItem menultem = new JMenuItem("内部框架窗口");
menu.add(menultem);
menuBar.add(menu);
setJMenuBar(menuBar);
getContentPane().add(desktopPane);
menultem.addActionListener(this);
setSize(350, 200);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
JInternalFrame inFrame = new JInternalFrame("内部框架(圆环)", true, true, true, true);
Container c = inFrame.getContentPane();
CirclePanel circlePanel = new CirclePanel();
JLabel label = new JLabel("圆环");
c.add(circlePanel, BorderLayout.CENTER);
c.add(label, BorderLayout.WEST);
int w = circlePanel.getImageWidthHeight().width + 150;
int h = circlePanel.getImageWidthHeight().height + 50;
inFrame.setSize(w, h);
inFrame.reshape(100, 50, w, h);
inFrame.setOpaque(true);
desktopPane.add(inFrame);
inFrame.setVisible(true);
}
public static void main(String args[]) {
MyDesktopPane app = new MyDesktopPane();
app.addWindowListener(new MyWindowListener());
}
}
class CirclePanel extends JPanel {
private ImageIcon imglcon;
public CirclePanel() {
try {
imglcon = new ImageIcon(new URL(""));
} catch (MalformedURLException ex) {
Logger.getLogger(CirclePanel.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void paint(Graphics g) {
g.drawImage(imglcon.getImage(), 0, 0, this);
}
public Dimension getImageWidthHeight() {
return new Dimension(imglcon.getIconWidth(), imglcon.getIconHeight());
}
}
class MyWindowListener extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(1);
}
}
另外,团IDC网上有许多产品团购,便宜有口碑
final static JDesktopPane desktopPane = new JDesktopPane();
public MyDesktopPane() {
super("MyDesktopPane.java:DesktopPane测试");
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("新增窗口");
JMenuItem menultem = new JMenuItem("内部框架窗口");
menu.add(menultem);
menuBar.add(menu);
setJMenuBar(menuBar);
getContentPane().add(desktopPane);
menultem.addActionListener(this);
setSize(350, 200);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
JInternalFrame inFrame = new JInternalFrame("内部框架(圆环)", true, true, true, true);
Container c = inFrame.getContentPane();
CirclePanel circlePanel = new CirclePanel();
JLabel label = new JLabel("圆环");
c.add(circlePanel, BorderLayout.CENTER);
c.add(label, BorderLayout.WEST);
int w = circlePanel.getImageWidthHeight().width + 150;
int h = circlePanel.getImageWidthHeight().height + 50;
inFrame.setSize(w, h);
inFrame.reshape(100, 50, w, h);
inFrame.setOpaque(true);
desktopPane.add(inFrame);
inFrame.setVisible(true);
}
public static void main(String args[]) {
MyDesktopPane app = new MyDesktopPane();
app.addWindowListener(new MyWindowListener());
}
}
class CirclePanel extends JPanel {
private ImageIcon imglcon;
public CirclePanel() {
try {
imglcon = new ImageIcon(new URL(""));
} catch (MalformedURLException ex) {
Logger.getLogger(CirclePanel.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void paint(Graphics g) {
g.drawImage(imglcon.getImage(), 0, 0, this);
}
public Dimension getImageWidthHeight() {
return new Dimension(imglcon.getIconWidth(), imglcon.getIconHeight());
}
}
class MyWindowListener extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(1);
}
}
另外,团IDC网上有许多产品团购,便宜有口碑
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public class MyDesktopPane extends JFrame implements ActionListener {
final static JDesktopPane desktopPane = new JDesktopPane();
public MyDesktopPane() {
super("MyDesktopPane.java:DesktopPane测试");
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("新增窗口");
JMenuItem menultem = new JMenuItem("内部框架窗口");
menu.add(menultem);
menuBar.add(menu);
setJMenuBar(menuBar);
getContentPane().add(desktopPane);
menultem.addActionListener(this);
setSize(350, 200);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
JInternalFrame inFrame = new JInternalFrame("内部框架(圆环)", true, true, true, true);
Container c = inFrame.getContentPane();
CirclePanel circlePanel = new CirclePanel();
JLabel label = new JLabel("圆环");
c.add(circlePanel, BorderLayout.CENTER);
c.add(label, BorderLayout.WEST);
int w = circlePanel.getImageWidthHeight().width + 150;
int h = circlePanel.getImageWidthHeight().height + 50;
inFrame.setSize(w, h);
inFrame.reshape(100, 50, w, h);
inFrame.setOpaque(true);
desktopPane.add(inFrame);
inFrame.setVisible(true);
}
public static void main(String args[]) {
MyDesktopPane app = new MyDesktopPane();
app.addWindowListener(new MyWindowListener());
}
}
class CirclePanel extends JPanel {
private ImageIcon imglcon;
public CirclePanel() {
try {
imglcon = new ImageIcon(new URL(""));
} catch (MalformedURLException ex) {
Logger.getLogger(CirclePanel.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void paint(Graphics g) {
g.drawImage(imglcon.getImage(), 0, 0, this);
}
public Dimension getImageWidthHeight() {
return new Dimension(imglcon.getIconWidth(), imglcon.getIconHeight());
}
}
class MyWindowListener extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(1);
}
}
final static JDesktopPane desktopPane = new JDesktopPane();
public MyDesktopPane() {
super("MyDesktopPane.java:DesktopPane测试");
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("新增窗口");
JMenuItem menultem = new JMenuItem("内部框架窗口");
menu.add(menultem);
menuBar.add(menu);
setJMenuBar(menuBar);
getContentPane().add(desktopPane);
menultem.addActionListener(this);
setSize(350, 200);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
JInternalFrame inFrame = new JInternalFrame("内部框架(圆环)", true, true, true, true);
Container c = inFrame.getContentPane();
CirclePanel circlePanel = new CirclePanel();
JLabel label = new JLabel("圆环");
c.add(circlePanel, BorderLayout.CENTER);
c.add(label, BorderLayout.WEST);
int w = circlePanel.getImageWidthHeight().width + 150;
int h = circlePanel.getImageWidthHeight().height + 50;
inFrame.setSize(w, h);
inFrame.reshape(100, 50, w, h);
inFrame.setOpaque(true);
desktopPane.add(inFrame);
inFrame.setVisible(true);
}
public static void main(String args[]) {
MyDesktopPane app = new MyDesktopPane();
app.addWindowListener(new MyWindowListener());
}
}
class CirclePanel extends JPanel {
private ImageIcon imglcon;
public CirclePanel() {
try {
imglcon = new ImageIcon(new URL(""));
} catch (MalformedURLException ex) {
Logger.getLogger(CirclePanel.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void paint(Graphics g) {
g.drawImage(imglcon.getImage(), 0, 0, this);
}
public Dimension getImageWidthHeight() {
return new Dimension(imglcon.getIconWidth(), imglcon.getIconHeight());
}
}
class MyWindowListener extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(1);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
把ClassTry加一个JTextField成员变量,调用的时候将窗体中的JTextField传递给ClassTry就可以了。
public class ClassTry {
private JTextField jt;
public ClassTry(JTextField jt)
{
this.jt = jt;
}
public void addText()
{
jt.setText("Hello World");
}
}
事件调用的时候:
ClassTry CT=new ClassTry(textField);
CT.addText();
沈阳冠尔Java培训,软件工程师的黄埔军校!
public class ClassTry {
private JTextField jt;
public ClassTry(JTextField jt)
{
this.jt = jt;
}
public void addText()
{
jt.setText("Hello World");
}
}
事件调用的时候:
ClassTry CT=new ClassTry(textField);
CT.addText();
沈阳冠尔Java培训,软件工程师的黄埔军校!
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你需要把要添加内容的txtxfiled传过去啊、、要不然砸设置值啊!!
ClassTry CT=new ClassTry(textField );
在ClassTry里面构造一下,就可以了赛。。。
ClassTry CT=new ClassTry(textField );
在ClassTry里面构造一下,就可以了赛。。。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询