如何在Java中删除MigLayout上的自动插入?

时间:2016-01-07 19:13:52

标签: java swing user-interface jpanel miglayout

我正在使用MigLayout创建UI。我的问题是,如何摆脱屏幕上的灰色/白色插图并使小程序的整个背景变成深褐色? (53,5,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();

}

}

2 个答案:

答案 0 :(得分:1)

只需创建一个面板并将其设置为JFrame的内容窗格即可。

你的样本非常复杂,不能运行,因为它引用了图像和声音文件......这是一个简单的例子,可以让你更好地了解如何实现你想要的东西:

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();
    }
}

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

答案 1 :(得分:0)

只需在布局约束中将insets设置为0,如"insets 0"