Java难题 是关于窗体创建的 下面的程序运行会出现两个窗体,怎么样才能合并成一个?? 30
importjavax.swing.*;importjava.util.concurrent.*;publicclassSubmitSwingProgramextends...
import javax.swing.*;
import java.util.concurrent.*;
public class SubmitSwingProgram extends JFrame
{
JLabel label;
public static void run(final JFrame f, final int width, final int height)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
f.setTitle(f.getClass().getSimpleName());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(width, height);
f.setVisible(true);
}
});
}
public SubmitSwingProgram()
{
run(new JFrame(), 500, 300);
label = new JLabel("A Label");
add(label);
setVisible(true);
}
static SubmitSwingProgram ssp;
public static void main(String[] args) throws Exception
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
ssp = new SubmitSwingProgram();
}
});
TimeUnit.SECONDS.sleep(1);
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
ssp.label.setText("This is different");
}
});
}
} 展开
import java.util.concurrent.*;
public class SubmitSwingProgram extends JFrame
{
JLabel label;
public static void run(final JFrame f, final int width, final int height)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
f.setTitle(f.getClass().getSimpleName());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(width, height);
f.setVisible(true);
}
});
}
public SubmitSwingProgram()
{
run(new JFrame(), 500, 300);
label = new JLabel("A Label");
add(label);
setVisible(true);
}
static SubmitSwingProgram ssp;
public static void main(String[] args) throws Exception
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
ssp = new SubmitSwingProgram();
}
});
TimeUnit.SECONDS.sleep(1);
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
ssp.label.setText("This is different");
}
});
}
} 展开
4个回答
2009-10-14
展开全部
SubmitSwingProgram 继承了JFrame,本身就是一个窗口
构造方法中
public SubmitSwingProgram()
{
run(new JFrame(), 500, 300);//这里又new了一个窗口
label = new JLabel("A Label");
add(label);
setVisible(true);//这里你让SubmitSwingProgram显示
}
当然会出现两个窗口
你到底要几个窗口?你的代码一团糟,多看看例子
构造方法中
public SubmitSwingProgram()
{
run(new JFrame(), 500, 300);//这里又new了一个窗口
label = new JLabel("A Label");
add(label);
setVisible(true);//这里你让SubmitSwingProgram显示
}
当然会出现两个窗口
你到底要几个窗口?你的代码一团糟,多看看例子
展开全部
晕 看代码中就都被回答完了……
以下修复后的代码
import javax.swing.*;
import java.awt.Dimension;
import java.util.concurrent.*;
public class SubmitSwingProgram extends JFrame {
JLabel label;
public static void runx(final JFrame f, final int width, final int height) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
f.setTitle(f.getClass().getSimpleName());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setPreferredSize(new Dimension(height, width));
f.setVisible(true);
}
});
}
public SubmitSwingProgram() {
runx(this, 500, 300);
label = new JLabel("A Label");
add(label);
setVisible(true);
}
static SubmitSwingProgram ssp;
public static void main(String[] args) throws Exception {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
ssp = new SubmitSwingProgram();
}
});
TimeUnit.SECONDS.sleep(1);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
ssp.label.setText("This is different");
}
});
}
}
以下修复后的代码
import javax.swing.*;
import java.awt.Dimension;
import java.util.concurrent.*;
public class SubmitSwingProgram extends JFrame {
JLabel label;
public static void runx(final JFrame f, final int width, final int height) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
f.setTitle(f.getClass().getSimpleName());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setPreferredSize(new Dimension(height, width));
f.setVisible(true);
}
});
}
public SubmitSwingProgram() {
runx(this, 500, 300);
label = new JLabel("A Label");
add(label);
setVisible(true);
}
static SubmitSwingProgram ssp;
public static void main(String[] args) throws Exception {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
ssp = new SubmitSwingProgram();
}
});
TimeUnit.SECONDS.sleep(1);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
ssp.label.setText("This is different");
}
});
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
构造函数
public SubmitSwingProgram()
{
run(new JFrame(), 500, 300);
label = new JLabel("A Label");
add(label);
setVisible(true);
}
里面第一行改为:
run(this, 500, 300);
public SubmitSwingProgram()
{
run(new JFrame(), 500, 300);
label = new JLabel("A Label");
add(label);
setVisible(true);
}
里面第一行改为:
run(this, 500, 300);
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
写得真够乱
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询