gpt4 book ai didi

java - 为什么我的 Button 不会改变位置或移动?

转载 作者:行者123 更新时间:2023-12-01 10:22:23 25 4
gpt4 key购买 nike

我有这个有效的代码,但是每次我更改坐标时,它都不起作用!我已经尝试了几乎所有方法(希望有一个解决方案)来解决这个问题,但没有任何效果。我基本上希望按钮出现在标签旁边。

import java.util.*;
import java.awt.*;
import javax.swing.*;

public class jframetest
{
public static void main(String args[])
{
JFrame frame = new JFrame("Not Main");
frame.setPreferredSize(new Dimension(200, 200));

JLabel label = new JLabel("Test");

Random generate = new Random();
Random rand = new Random();

String[] name = {"Landry", "Azariah", "Oakley", "Lennon", "Charlie", "Skyler", "Dakota", "Armani", "Phoenix" , "Justice", "Casey", "Emory", "Remy", "Emerson", "Amari", "Roxie", "Hayden", "River", "Milan", "Tatum", "Jessie", "Finley", "Riley", "Rowan", "Sage", "Jamie", "Rory", "Harley", "Leighton", "Peyton", "Dallas", "Remington", "Quinn", "Alexis", "Sawyer", "Kamryn", "Parker", "Avery", "Eden", "Lyric", "Elliot", "Reese", "Zion", "Rylan", "Jordan", "Taylor", "Morgan", "Kendall", "Rylee", "Ryan", "Reagan", "Logan", "Hunter", "Carter"};
String[] images = new String[]{"image1.jpg", "image2.jpg", "image3.jpg", "image4.jpg", "image5.jpg", "image6.jpg", "image7.jpg", "image8.jpg", "image9.jpg", "image10.jpg", "image11.jpg", "image12.jpg", "image13.jpg"};

int index = (int) (Math.random() * (images.length - 1));

JButton button = new JButton(new ImageIcon(images[index]));
button.setPreferredSize(new Dimension(200, 200));
button.setBounds(5, 5, 100, 50);

JLabel label2 = new JLabel("Customer: " + name[generate.nextInt(54)]);
label2.setBounds(10, 10, 50, 25);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(button);
frame.setVisible(true);
frame.pack();
label.setText("Test");
frame.add(button);
frame.add(label);
frame.add(label2);
}
}

最佳答案

您可以将FlowLayout用于JFrame

// this line added
frame.setLayout(new FlowLayout(FlowLayout.LEFT));
frame.add(button);
frame.add(label);
frame.add(label2);

关于java - 为什么我的 Button 不会改变位置或移动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35495807/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com