gpt4 book ai didi

java - 如何摆脱 Java 中 MigLayout 的自动插入?

转载 作者:行者123 更新时间:2023-11-30 07:41:17 25 4
gpt4 key购买 nike

我正在使用 MigLayout 创建 UI。我的问题是,如何消除屏幕上的灰色/白色插图并使小程序的整个背景变成深棕色? (53,9,9)

我附上了一张照片,可以更好地解释我在下面遇到的问题。

enter image description here

Image of the Issue

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import net.miginfocom.swing.MigLayout;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Casino extends JFrame implements ActionListener {
private JButton start, settings, scenario, music;

//mainUI, startUI, settingsUI, scenarioUI, blackjackUI, oddorevenUI, tcmUI, overorunderUI, slotsUI;

/**
* Constructor method
*/

public Casino(){

JPanel mainUI, startUI, settingsUI, scenarioUI, blackjackUI, oddorevenUI, tcmUI, overorunderUI, slotsUI;
JPanel menus = new JPanel(new MigLayout());


MigLayout mig = new MigLayout();
mainUI = new JPanel(new MigLayout());
buildMainUI(mainUI);

menus.add(mainUI);
add(menus);



mig.layoutContainer(mainUI);



//Audio implementation Method 2(not mine)
Clip clip = null;
try {
clip = AudioSystem.getClip();
} catch (LineUnavailableException e1) {

e1.printStackTrace();
}
AudioInputStream inputStream = null;
try {
inputStream = AudioSystem.getAudioInputStream(new File("57.wav"));
} catch (UnsupportedAudioFileException e) {

e.printStackTrace();
} catch (IOException e) {

e.printStackTrace();
}
try {
clip.open(inputStream);
} catch (LineUnavailableException e) {

e.printStackTrace();
} catch (IOException e) {

e.printStackTrace();
}
clip.loop(Clip.LOOP_CONTINUOUSLY);
try {
Thread.sleep(10000);
} catch (InterruptedException e) {

e.printStackTrace();
} // looping as long as this thread is alive

/* Audio code taken from
* http://stackoverflow.com/questions/8979914/audio-clip-wont-loop-continuously
*/
setSize(780, 700);
setResizable(false);
setLayout(mig);
setTitle("White Lily Casino");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}

public void actionPerformed(ActionEvent e){

}

public void buildMainUI(JPanel mainUI){

getContentPane().add(mainUI);
mainUI.setBackground(new Color(53, 9, 9));


//Background items
JLabel title = new JLabel(new ImageIcon("title.png"));
mainUI.add(title, "dock north");

JLabel border = new JLabel(new ImageIcon("mainscreenborder.png"));
mainUI.add(border, "pos 0px 500px");

settings = new JButton();
ImageIcon s = new ImageIcon("settings-button.png");
settings.setBackground(new Color(53, 9, 9));
settings.setIcon(s);
mainUI.add(settings, "pos 300 200");

music = new JButton();
ImageIcon m = new ImageIcon("music-button.png");
music.setBackground(new Color(53, 9, 9));
music.setIcon(m);
mainUI.add(music, "pos 300 263");

scenario = new JButton();
ImageIcon sc = new ImageIcon("scenario-button.png");
scenario.setBackground(new Color(53, 9, 9));
scenario.setIcon(sc);
mainUI.add(scenario, "pos 300 326");

start = new JButton();
ImageIcon st = new ImageIcon("start-button.png");
start.setBounds(320, 404, 122, 63);
start.setBackground(new Color(53, 9, 9));
start.setIcon(st);
mainUI.add(start, "pos 300 389");
}

public void buildStartUI(JPanel startUI){

}

public void buildSettingsUI(JPanel settingsUI){

}

public void buildScenarioUI(JPanel scenarioUI){

}

public void buildBlackjackUI(JPanel blackjackUI){

}

public void buildOddOrEvenUI(JPanel oddorevenUIUI){

}

public void buildTCMUI(JPanel tcmUI){

}

public void buildOverOrUnderUI(JPanel overorunderUI){

}

public void buildSlotsUI(JPanel slotsUI){


}


public static void main(String[] args) {
// TODO Auto-generated method stub

Casino wlc = new Casino();

}

}

最佳答案

只需创建一个面板并将其设置为 JFrame 的内容 Pane 。

您的示例非常复杂并且无法运行,因为它引用了图像和声音文件...这是一个简单的示例,可以让您更好地了解如何实现您想要的目标:

import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import net.miginfocom.swing.MigLayout;

public class Casino extends JFrame
{

public Casino()
{
JPanel mainPanel = new JPanel (new MigLayout());
buildMainUI(mainPanel);
setContentPane(mainPanel);
setSize(780, 700);
setResizable(false);
setTitle("White Lily Casino");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}

public void buildMainUI(JPanel mainUI)
{
mainUI.setBackground(new Color(53, 9, 9));

// Background items
JLabel title = new JLabel("Title");
title.setForeground(Color.CYAN);
mainUI.add(title, "dock north");
}

public static void main(String[] args)
{
Casino wlc = new Casino();
}
}

附注由于您使用的是 miglayout,因此有比绝对坐标更好的方法来定位按钮。查看cheatsheetquickstart guide .

关于java - 如何摆脱 Java 中 MigLayout 的自动插入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34663300/

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