다시 나타납니다. GUI에서 이미지 위에 버튼을 놓는 방법에 대해 궁금합니다. 여기에 내 현재 코드 :이미지 위에 버튼을 놓습니다.
private static JPanel titlePanel = new JPanel();
private static JLabel titleScreen = new JLabel();
private static JLabel titleScreenBackground = new JLabel();
private static JButton startGameButton = new JButton("START GAME");
private static ImageIcon titleScreenPic = new ImageIcon("http://icdn6.digitaltrends.com/image/battleship-650x0.jpg");
private static JFrame frame=new JFrame(); //creates frame
public static void main(String[] args) throws MalformedURLException{
titleScreen();
}
public static void titleScreen() throws IOException{
titleScreen.setLayout(new GridBagLayout());
titlePanel.setLayout(new GridBagLayout());
GridBagConstraints c1 = new GridBagConstraints();
c1.gridx = 0;
c1.gridy = 0;
c1.anchor = GridBagConstraints.PAGE_END;
titleScreenBackground.setIcon(titleScreenPic);
titlePanel.add(startGameButton);
titlePanel.setAlignmentY(SwingConstants.BOTTOM);
frame.add(titleScreenBackground);
frame.add(titlePanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(630, 300); //sets appropriate size for frame
frame.setVisible(true); //makes frame visible
}
내가 GridBagLayout로 그래서 난 같은 셀에 구성 요소를 배치 할 수있는 패널을 만들려고하지만, 여전히 그것을 바로 옆에 첫번째 후 버튼을 이미지를 배치합니다.
편집 : 코드를 다시 작성하여 원하는대로 처리했습니다. 보시다시피 단추의 위치를 설정하려고하는 선은 단추에 아무 것도하지 않습니다.
가능한 [중복] (http://stackoverflow.com/q/10861852/230513). – trashgod
좋아, 나는 이미지에 버튼을 배치 관리,하지만 지금은 할 수 없습니다 그것들을 정렬합니다. 버튼에 .setAlignmentY를 사용하고 있는데, 여전히 가운데 상단에 있습니다. – user3741402
@ user3741402, 적절한 레이아웃 관리자를 사용하면 BorderLayout이 가장 쉽고 코드를 업데이트하여 변경 사항을 볼 수 있습니다. – camickr