я написал код, используя GridLayou, однако я не могу управлять, чтобы удалить серый фон и использовать IMG я подключен в качестве фона, и картина демонстрирует проблему:прозрачный фон GridLayout
Может кто-то, пожалуйста, помогите?
это код
public class mainClass {
private static JButton start;
static BackgroundPanel bp = null;
static JFrame mainf = null;
static int R;
final static boolean shouldFill = true;
// group turns boolean
boolean redTurn = false;
boolean blueTurn = false;
boolean greenTurn = false;
boolean yellowTurn = false;
// boards
static ImageIcon gameBoard;
static ImageIcon blueBoard;
static ImageIcon qBoard;
// dice
static JButton dice_1 = null;
public static void main(String[] args) throws IOException {
mainf = new JFrame ("سين جيم");
// background
BufferedImage mFrame = ImageIO.read(new File("B1.png"));
bp = new BackgroundPanel(mFrame);
mainf.add(bp);
bp.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
// c.fill = GridBagConstraints.HORIZONTAL;
// Hi string
JLabel hi = new JLabel ("أهلا وسهلا بكم في لعبة الليدو");
Font fs = hi.getFont();
hi.setFont(fs.deriveFont(50f));
bp.add(hi);
// empty
// button
start = new JButton ("لنبدأاللعب");
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 20; //make this component tall
c.weightx = 0.0;
c.gridwidth = 3;
c.gridx = 0;
c.gridy = 1;
c.insets = new Insets(10,0,0,0); //top padding
bp.add(start, c);
// Action Listener
start.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// starting the game area
// emptying all
bp.removeAll();
bp.revalidate();
bp.repaint();
BufferedImage mFrame2= null;
try {
// changing background
mFrame2 = ImageIO.read(new File("B2.png"));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// setting new background
bp = new BackgroundPanel(mFrame2);
bp.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
// adding gameBoard
gameBoard = new ImageIcon("gameBoard.png");
JLabel gameBoard_1 = new JLabel(gameBoard);
bp.add(gameBoard_1);
// adding blueBoard
blueBoard = new ImageIcon("blueBoard.png");
JLabel blueBoard_1 = new JLabel(blueBoard);
bp.add(blueBoard_1);
GridLayout experimentLayout = new GridLayout(4,4); // rows | , cols-
blueBoard_1.setLayout(experimentLayout);
// gameName panel
JPanel gameName = new JPanel();
gameName.setLayout(new GridLayout(1,0));
JLabel Ledu= new JLabel ("لعبة اللدو");
Font font3 = new Font("Verdana", Font.BOLD, 30);
Ledu.setFont(font3);
Ledu.setForeground(Color.WHITE);
gameName.add(Ledu);
// teamPanel
JPanel teamName = new JPanel();
teamName.setLayout(new GridLayout(2,0));
JLabel blue= new JLabel ("الفريق الأرزق");
Font font2 = new Font("Verdana", Font.BOLD, 20);
blue.setFont(font2);
blue.setForeground(Color.BLUE);
JLabel red= new JLabel ("الفريق الأحمر");
red.setFont(font2);
red.setForeground(Color.RED);
JLabel yellow= new JLabel ("الفريق الأصفر");
yellow.setFont(font2);
yellow.setForeground(Color.YELLOW);
JLabel green= new JLabel ("الفريق الأخضر");
green.setFont(font2);
green.setForeground(Color.GREEN);
// team panel
teamName.add(blue);
teamName.add(red);
teamName.add(yellow);
teamName.add(green);
// adding question
JPanel question = new JPanel();
question.setLayout(new GridLayout(3,2));
ImageIcon q = new ImageIcon("Q.png");
JLabel q_1 = new JLabel(q);
// adding Question panel
question.add(q_1);
// dicePanel
final JPanel dicePanel = new JPanel();
dicePanel.setLayout(new GridLayout(4,0));
ImageIcon dice = new ImageIcon("dice.png");
dice_1 = new JButton(dice);
// adding dice panel
dicePanel.add(dice_1);
dice_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dicePanel.remove(dice_1);
dicePanel.revalidate();
dicePanel.repaint();
ImageIcon dice = new ImageIcon("dice.gif");
dice_1 = new JButton(dice);
dicePanel.add(dice_1);
// random number
Random r = new Random();
R = r.nextInt(10) + 2;
System.out.println(R);
}
});
// Centeralizing Ledu
Ledu.setHorizontalAlignment(SwingConstants.CENTER);
gameName.setAlignmentX(Component.CENTER_ALIGNMENT);
blue.setHorizontalAlignment(SwingConstants.CENTER);
red.setHorizontalAlignment(SwingConstants.CENTER);
yellow.setHorizontalAlignment(SwingConstants.CENTER);
green.setHorizontalAlignment(SwingConstants.CENTER);
teamName.setAlignmentX(Component.CENTER_ALIGNMENT);
// no backgrounf color
Ledu.setOpaque(false);
gameName.setOpaque(false);
blue.setOpaque(false);
red.setOpaque(false);
yellow.setOpaque(false);
green.setOpaque(false);
teamName.setOpaque(false);
question.setOpaque(false);
dicePanel.setOpaque(false);
// final add
blueBoard_1.add(gameName, BorderLayout.PAGE_START);
blueBoard_1.add(teamName, BorderLayout.CENTER);
blueBoard_1.add(question, BorderLayout.LINE_END);
blueBoard_1.add(dicePanel, BorderLayout.PAGE_END);
/*
// add dice
ImageIcon dice = new ImageIcon("dice.png");
dice_1 = new JButton(dice);
dice_1.setSize(100, 70);
bp.add(dice_1);
// changin to moving dice
dice_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
bp.remove(dice_1);
bp.revalidate();
bp.repaint();
ImageIcon dice = new ImageIcon("dice.gif");
dice_1 = new JButton(dice);
dice_1.setSize(100, 70);
bp.add(dice_1);
// random number
Random r = new Random();
int R = r.nextInt(10) + 2;
System.out.println(R);
}
});
*/
mainf.setContentPane(bp);
};}); // end of start actionListener
mainf.pack();
mainf.setVisible(true);
};
// dice class
}
добавил код сейчас :) – Neyon
@Neyon, 'любая идея как отрегулировать размер столбца, чтобы я мог просмотреть img, я хочу, чтобы он просматривался?' В каком столбце? Какое изображение? Ваш вопрос касался отображения ярлыков на синем фоне. Я понятия не имею, что вы сейчас спрашиваете. – camickr
да это другой вопрос если вы посмотрите на это изображение: http://puu.sh/k4AoT/d21707be38.png u можете сказать, что img ниже (http://puu.sh/k4E0s/4c1cf9e53b. png) является неполным, любая идея, как настроить это? – Neyon