AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

Visuelle Styles in Java

Ein Thema von 3_of_8 · begonnen am 7. Jul 2006 · letzter Beitrag vom 8. Jul 2006
 
Benutzerbild von 3_of_8
3_of_8

Registriert seit: 22. Mär 2005
Ort: Dingolfing
4.129 Beiträge
 
Turbo Delphi für Win32
 
#5

Re: Visuelle Styles in Java

  Alt 8. Jul 2006, 10:51
So siehts aus (egal ob mit oder ohne UIManager.SetLookAndFeel).

Die Komponenten, die gestylt werden sollen sind ausschließlich JButtons, JPanels, JFrames. Und: Ich habe rausgefunden, welche Look and Feels bei mir installiert sind:
Metal javax.swing.plaf.metal.MetalLookAndFeel
CDE/Motif com.sun.java.swing.plaf.motif.MotifLookAndFeel
Windows com.sun.java.swing.plaf.windows.WindowsLookAndFeel
Windows Classic com.sun.java.swing.plaf.windows.WindowsClassicLook AndFeel

Mein Code sieht so aus:

Code:
package gui;

import javax.swing.ComponentInputMap;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.LookAndFeel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.JFileChooser;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.Component;
import java.awt.Container;
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Dimension;
import java.awt.BorderLayout;
import java.io.FileNotFoundException;
import java.io.IOException;

import sudoku.NotSolvableException;
import sudoku.HelpingState;

public class SudokuGUI extends JFrame {
   
    private static final String LOOK_AND_FEEL_ERROR_MESSAGE =
            "Could not initiate look and feel. Will now "
            + "use default look and feel.";

    private static final String INVALID_FILE_MESSAGE = "Could not read file.";

    private static final String LOAD_DIALOG_CAPTION = "Load sudoku";

    private static final String NO_MORE_SOLVABLE_MESSAGE = "Sudoku no more solvable. "
                                + "Use undo or unset to correct the mistakes.";

    private static final String ERROR_CAPTION = "Error";

    private static final String SOLVED_MESSAGE = "Congratulations! The sudoku is solved.";

    private static final String SOLVED_CAPTION = "Sudoku solved";

    private static final String ALREADY_SOLVED_MESSAGE = "The sudoku is already solved.";

    private static final String ALREADY_SOLVED_CAPTION = "Sudoku already solved";

    private static final String CLOSE_BUTTON_CAPTION = "Close";

    private static final String LOAD_BUTTON_CAPTION = "Load";

    private static final String UNDO_BUTTON_CAPTION = "Undo";

    private static final String FILE_NOT_FOUND_MESSAGE =
            "File %s does not exist.";
   
    private static final String HELP_BUTTON_CAPTION =
            "Help";
   
    private HelpingState s;
    private SudokuBoard board;

    public static void main(String[] args) {
        new SudokuGUI();
    }
   
    public void showMessage(String title, String msg) {
        showMessage(title, msg, JOptionPane.PLAIN_MESSAGE);
    }
   
    public void showMessage(String title, String msg, int type) {
        JOptionPane.showMessageDialog(this, msg, title, type);
    }
   
    public SudokuGUI() {
        s = new HelpingState();
        this.setTitle("Sudoku");
       
        for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
            System.out.println(info);
        }
       
        addComponentListener(new ComponentListener() {
            public void componentResized(ComponentEvent e) {
                JFrame frame = (JFrame) e.getSource();
                if (frame.getWidth() < 280) {
                    frame.setSize(280, frame.getHeight());
                }
                if (frame.getHeight() < 320) {
                    frame.setSize(frame.getWidth(), 320);
                }
            }           
            public void componentMoved(ComponentEvent e) {}
            public void componentHidden(ComponentEvent e) {}
            public void componentShown(ComponentEvent e) {}
        });
        setMinimumSize(new Dimension(160, 180));
        Container cp = getContentPane();
        setLayout(new BorderLayout(20, 20));
        board = new SudokuBoard();
        board.state = s;
        JPanel panel = new JPanel();
        JPanel buttonPanel = new JPanel();
        cp.add(board, BorderLayout.CENTER);
        cp.add(panel, BorderLayout.SOUTH);
        Button help = new Button(HELP_BUTTON_CAPTION);
        Button undo = new Button(UNDO_BUTTON_CAPTION);
        Button load = new Button(LOAD_BUTTON_CAPTION);
        Button close = new Button(CLOSE_BUTTON_CAPTION);
        panel.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));
        panel.add(buttonPanel);
        buttonPanel.setLayout(new GridLayout(1, 4, 20, 20));
        buttonPanel.add(help);
        buttonPanel.add(undo);
        buttonPanel.add(load);
        buttonPanel.add(close);
       
        help.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                   
                    if (s.isSolution()) {
                        showMessage(ALREADY_SOLVED_CAPTION,
                                ALREADY_SOLVED_MESSAGE);
                        board.repaint();
                        return;
                    }
                   
                    s.getHelp();
                                       
                    if (s.isSolution()) {
                        showMessage(SOLVED_CAPTION,
                                SOLVED_MESSAGE);
                    }
                } catch (NotSolvableException ex) {
                    showMessage(ERROR_CAPTION, NO_MORE_SOLVABLE_MESSAGE,
                            JOptionPane.ERROR_MESSAGE);
                }
                board.repaint();
            }
        });
       
        undo.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                s.undo();
                board.repaint();
            }
        });
       
        load.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JFileChooser fileChooser = new JFileChooser();
                fileChooser.setDialogTitle(LOAD_DIALOG_CAPTION);
                if (fileChooser.showOpenDialog(null) ==
                        JFileChooser.APPROVE_OPTION) {
                    try {
                        s.loadFromFile(fileChooser.getSelectedFile().
                                getAbsolutePath());
                        board.repaint();
                    } catch (FileNotFoundException ex) {
                        showMessage(ERROR_CAPTION,
                                String.format(FILE_NOT_FOUND_MESSAGE,
                                fileChooser.getSelectedFile().getPath()),
                                JOptionPane.ERROR_MESSAGE);
                    } catch (IOException ex) {
                        showMessage(ERROR_CAPTION,
                                INVALID_FILE_MESSAGE,
                                JOptionPane.ERROR_MESSAGE);
                    } catch (NotSolvableException ex) {
                        showMessage(ERROR_CAPTION,
                                "Input file is contradictory or invalid.",
                                JOptionPane.ERROR_MESSAGE);
                    }
                }
            }
        });
       
        close.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        }
        );
       
        setSize(500, 600);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
   
}
Angehängte Grafiken
Dateityp: png sudoku_gui_screenshot_207.png (26,6 KB, 41x aufgerufen)
Manuel Eberl
„The trouble with having an open mind, of course, is that people will insist on coming along and trying to put things in it.“
- Terry Pratchett
  Mit Zitat antworten Zitat
 


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 11:41 Uhr.
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz