0

.getComponents()そうです、これは を返す Component を使用することで比較的簡単になると思いましたJPanelが、困難に直面 しています。JOptionPaneJButtonJPanel

JOptionPaneロールオーバー時にボタンの色を変更できるように、ボタンでマウス リスナーを使用したいと考えています。これを達成するためのより簡単な方法はありますか?

これはこれまでの私のクラスです..

package rsapp.gui;

import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;


import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.UIManager;


public class RSJPaneComponent extends JOptionPane {

    /**
     * 
     */
    private static final long serialVersionUID = 13453253L;
    private JOptionPane j=this;
    final Color WHITE = Color.WHITE;


    public RSJPaneComponent(){
        UIManager.put("OptionPane.background",WHITE);
        UIManager.put("Panel.background",WHITE);
        UIManager.put("Button.background",WHITE);
        UIManager.put("Button.foreground",new Color(85,153,187));
        UIManager.put("activeCaption", WHITE);
    }

    protected String initJPaneInput(final JFrame f, final String message){
        return j.showInputDialog(f,message);
    }

    public int generateDialog(int error_code, String title_message, String message, final JFrame f){
        return  JOptionPane.showConfirmDialog(
                f,
                message,
                "Error "+error_code+": "+title_message,
                JOptionPane.YES_NO_OPTION);
    }
}
4

1 に答える 1

5

これを達成するためのより簡単な方法はありますか?

を使用しJDialogます。長い経験から、JOptionPaneは強力で便利なコンポーネントですが、カスタマイズに関しては、 を使用するだけの方がよいことがわかりますJDialog


コード

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

class CustomDialog {

    public static void main(String[] args) {

        Runnable r = new Runnable() {
            public void run() {
                JFrame frame = new JFrame("Show Custom Dialog");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setSize(400,400);
                frame.setLocationRelativeTo(null);

                final JDialog dialog = new JDialog(frame, "Dialog", true);

                JPanel mainGui = new JPanel(new BorderLayout());
                mainGui.setBorder(new EmptyBorder(20,20,20,20));
                mainGui.add( new JLabel("Contents go here"), BorderLayout.CENTER );

                JPanel buttonPanel = new JPanel(new FlowLayout());
                mainGui.add(buttonPanel, BorderLayout.SOUTH);

                JButton close = new JButton("Close");
                close.addActionListener( new ActionListener(){
                    public void actionPerformed(ActionEvent ae) {
                        dialog.setVisible(false);
                    }
                } );

                buttonPanel.add(close);

                frame.setVisible(true);

                dialog.setContentPane(mainGui);
                dialog.pack();
                dialog.setLocationRelativeTo(frame);
                dialog.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}

スクリーンショット

ここに画像の説明を入力


この例には、 に組み込まれているすべての機能がまだ含まれていないことに注意してくださいJOptionPane

たとえば、 aJOptionPaneが開いているときにユーザーがエスケープ キーを押すと、ダイアログは閉じられます。KeyListenerまたはを使用してその機能を追加できますActionMap

于 2011-04-16T22:58:00.660 に答える