怎么SwingUtilities.invokeLater办?它只是推迟在其run方法中执行一段代码吗?在invokeLater函数中调用一个动作或者在我们想要执行的线程的末尾调用它有什么区别?任何人都可以帮我完成这项invokeLater功能的真正功能吗?
该程序使球从左上角到右下角滑动并起作用.但是,如果我要改变界限
frame.getContentPane().add(ball);
Run Code Online (Sandbox Code Playgroud)
从当前位置到for循环之后,为什么球不会出现在框架上.我同意球不应再移动,因为在我将球添加到JFrame之前,所有在for循环中完成的移动都会发生,但是我不明白为什么当我最终将球出现在屏幕上时将其添加到框架中.这是工作程序的代码,如果你将上面提到的行移到for循环之后,球就不再出现了
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Animate
{
private JFrame frame;
private int x,y;
public static void main(String args[])
{
Animate ballRoll = new Animate();
ballRoll.go();
}
public void go()
{
frame = new JFrame();
frame.setSize(500,500);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MyRoll ball = new MyRoll();
frame.getContentPane().add(ball);
for(x = 5;x<=350;x++)
{
y=x;
try
{
Thread.sleep(50);
}
catch(Exception e)
{
System.out.println("dsfsd");
}
ball.repaint();
}
}
class MyRoll extends JPanel
{
public void paintComponent(Graphics g)
{
g.setColor(Color.BLACK);
g.fillRect(0, 0, this.getWidth(), …Run Code Online (Sandbox Code Playgroud) 第一张图显示了我刚开始时GUI的样子,第二张图显示了当我点击电路板时会发生什么.在我点击一个片段然后单击顶行上的一个按钮后,棋子会显示在顶行.这里发生了什么?!

代码如下; 这个类是我拥有大部分代码的地方.其余的类只是在这一点上加载图像.在main中调用Board构造函数来构建GUI.
public class BoardPanel extends JPanel {
public BoardPanel() {
createBoard();
}
private void createBoard(){
setLayout(new GridLayout(10, 10));
// Makes a 10 x 10 grid of black and white colors
for (int i = 0; i<10; i++){
for (int j = 0; j<10; j++){
square[i][j] = new JButton();
square[i][j].setRolloverEnabled(false);
if ((i+j)%2 == 0)
square[i][j].setBackground(Color.WHITE);
else
square[i][j].setBackground(Color.LIGHT_GRAY);
add(square[i][j]);
}
}
addLabels();
//Colors the corner squares
square[0][0].setBackground(new Color(155, 234, 242, 100));
square[0][9].setBackground(new Color(155, 234, 242, 100));
square[9][0].setBackground(new Color(155, 234, 242, …Run Code Online (Sandbox Code Playgroud) 我试图做一个只显示图像的程序,一切正常,但控制台显示此警告...图像显示正常但我想知道什么是控制台警告以及如何解决它在这里我的主要课程......
public class main extends JFrame{
Image ryu;
imagen objRyu;
public main(){
super("Imagen1");
this.setSize(500,500);
this.setVisible(true);
this.setResizable(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
objRyu = new imagen(ryu,"images/Ryu.png",100,100);
repaint();
}
public static void main(String[] args) {
new main();
}
public void paint(Graphics g){
Graphics g2 = (Graphics2D)g;
g2.setColor(Color.gray);
g2.fillRect(0, 0, 500, 500);
g2.drawImage(objRyu.getImagen(), 100, 50, objRyu.getAncho(), objRyu.getAlto(), null);
}
}
Run Code Online (Sandbox Code Playgroud)
我的imagen课
public class imagen {
InputStream imgStream;
private Image imagen;
private int ancho;
private int alto;
public imagen(Image imagen, String ruta,int ancho, int alto){
this.imagen = imagen; …Run Code Online (Sandbox Code Playgroud) 我有几个问题:
首先,也是最重要的一点,为什么你可以多次运行带有框架,几个面板,按钮等的GUI程序 - 不做任何改动 - 一半时间组件显示在框架中,另一半时间什么都没有显示......从字面上看,我在代码中没有任何改变,这是最令人沮丧的问题.有时出现的东西,有时没什么.我正在使用Eclipse,并且在main方法中只有以下代码.
其次,有人可以清楚地解释框架,面板和布局如何工作?
JFrame f = new JFrame();
f.setSize(400, 400);
f.setLayout(new BorderLayout());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLocationRelativeTo(null);
f.setVisible(true);
JPanel p = new JPanel();
JButton alpha = new JButton();
JButton bravo = new JButton();
alpha.setLabel("Alpha");
bravo.setLabel("Bravo");
p.add(alpha, BorderLayout.WEST);
p.add(bravo, BorderLayout.EAST);
p.add(new JLabel("Charlie"), BorderLayout.SOUTH);
f.add(p);
Run Code Online (Sandbox Code Playgroud)
上面的方法有时会起作用,当它出现时,它并没有说明我从Oracle页面和教程中理解的方式.我创建了一个框架(没有默认布局),创建一个面板,其中两个按钮应该彼此相邻(WEST,EAST),然后在面板底部添加一个标签 - 并将整个面板添加到框架中.然而,它们都在框架的顶部彼此相邻地添加,居中.
你每个面板只能有一个组件吗?你可以在一个框架中添加多个面板,如果是这样,它们是否重叠?
仅供参考我观看了YouTube视频,并在此处阅读了其他一些帖子,似乎没有什么能够简单彻底地解释基础知识,所以我认为论坛帖子会得到更好的解释.
谢谢