2010-02-05 2 views
4

createDialog를 사용하여 JOptionPane에 세로로 세 개의 버튼을 쌓으 려하고 있지만 GridLayout에서는 제대로 작동하지 않습니다. 또한 '확인'버튼도 제거하는 방법을 잘 모르겠습니다. 당신은 아마 내가 왜 이렇게하고 있는지 궁금해 할 것입니다. 그러나 이것이 제가하는 말입니다. JFrame을 사용할 수 있다고 생각하지만 JOptionPane과 잘 어울리지 않는다고 생각합니다. 버튼을 쌓아두기 때문입니다.JDialogs를 사용하여 JOptionPane에 수직으로 버튼을 어떻게 쌓아 올립니까?

그것은 다음과 같이해야한다 :
를 | 도움이 필요하십니까 |
| 도와주세요 |
| 계산 |

나는 어떤 시점에서 액션 청취자를 추가하는 접근이 필요하지만, 심지어 그 지점에 도착하기 전에이 복잡한을 받고있는 것으로 보인다.

import java.awt.Container; 
import java.awt.GridLayout; 

import javax.swing.*; 
public class ThreeButtons { 

    static JDialog dialog; 
    public static void main(String[] args) { 

     JOptionPane optionPane = new JOptionPane(); 
     optionPane.setMessage("Set Message"); 
     optionPane.setMessageType(JOptionPane.INFORMATION_MESSAGE); 
     optionPane.setLayout(new GridLayout(3,1)); 
     String[] buttonTxt = {"Need Help","Help Me","Counting"}; 
     JButton[] buttons = new JButton[buttonTxt.length]; 
     for (int i = 0; i < buttonTxt.length; i++) 
     { 
      buttons[i] = new JButton(buttonTxt[i]); 
      optionPane.add(buttons[i]); 
     } 
     dialog = optionPane.createDialog(null, "Icon/Text Button"); 
     dialog.setVisible(true); 

    } 

} 

답변

10

당신은 당신이 패널에 추가하고이 같은 옵션 창에 패널을 추가 할 필요가 버튼 스택하려면 : 나는 당신이 제거 얻을 수있는 방법을 모르겠어요

JDialog dialog = null; 
    JOptionPane optionPane = new JOptionPane(); 
    optionPane.setMessage("Set Message"); 
    optionPane.setMessageType(JOptionPane.INFORMATION_MESSAGE); 

    JPanel panel = new JPanel(); 
    panel.setLayout(new GridLayout(3,1)); 
    String[] buttonTxt = {"Need Help","Help Me","Counting"}; 
    JButton[] buttons = new JButton[buttonTxt.length]; 
    for (int i = 0; i < buttonTxt.length; i++) 
    { 
     buttons[i] = new JButton(buttonTxt[i]); 
     panel.add(buttons[i]); 
    } 
    optionPane.setOptionType(JOptionPane.DEFAULT_OPTION); 
    optionPane.add(panel); 
    dialog = optionPane.createDialog(null, "Icon/Text Button"); 
    dialog.setVisible(true); 

을 JOptionPane의 내용을 수동으로 살펴보고 제거하는 것 외에는 OK 버튼을 누르십시오. 당신은 항상 당신의 자신의 JDialog를 다음 당신이 모든 권한을 만들 수 있지만, 좋은 joption 창 아이콘지고 약간 더 많은 작업 : 우리는 약간의 수정을 사용하여 'OK'버튼을 제거 할 수

+0

감사합니다 !!!!!!!!!!!! – user266840

+0

문제 없습니다. 이제 나에게 큰 뚱뚱한 upvote주고 옳은 대답으로 표시합니다. :) – willcodejavaforfood

1

있을 것입니다. 그냥 위치를 지정하는 옵션 창에 패널을 추가 할 때

 JDialog dialog = null; 
     JOptionPane optionPane = new JOptionPane(); 
     optionPane.setMessage("Set Message"); 
     optionPane.setMessageType(JOptionPane.INFORMATION_MESSAGE); 

     JPanel panel = new JPanel(); 
     panel.setLayout(new GridLayout(3,1)); 
     String[] buttonTxt = {"Need Help","Help Me","Counting"}; 
     JButton[] buttons = new JButton[buttonTxt.length]; 
     for (int i = 0; i < buttonTxt.length; i++) 
     { 
      buttons[i] = new JButton(buttonTxt[i]); 
      panel.add(buttons[i]); 
     } 
     optionPane.setOptionType(JOptionPane.DEFAULT_OPTION); 
     optionPane.add(panel,1); 
     dialog = optionPane.createDialog(null, "Icon/Text Button"); 
     dialog.setVisible(true); 

는 (이 경우에는 1 : 그 중간에 의미) .Therefore '확인'버튼을 내려갑니다.

+0

이것은 OK 버튼을 제거하지 않고 그냥 아래로 이동합니다. – Torque

관련 문제