【发布时间】:2016-04-12 07:47:18
【问题描述】:
我正在使用 MigLayout 创建 UI。我的问题是,我如何摆脱屏幕上的灰色/白色插图并使小程序的整个背景变成深棕色? (53, 9, 9)
我附上了一张可以更好地解释我在下面遇到的问题的照片。
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();
}
}
【问题讨论】:
-
我附上了一张可以更好地解释我在下面遇到的问题的照片。Runnable Example 可以更好地解释它。请记住:“一张图片包含超过 100 个单词,但您的代码包含超过 100 个图片”:)
-
添加了一个可运行的例子
-
如果您能够摆脱插图并在 Windows、X11 和 OS X 上取得良好的效果,我很想知道它是如何完成的 :)
-
出现这些“插图”是因为您使用的是
JButtonss,并且这种组件默认有边框。 -
当我说 inset 时,我指的是整个 JPanel 边界周围的灰色部分
标签: java swing user-interface jpanel miglayout