0

grouplayout を使用して JPanel を作成しようとしていますが、それでも同じエラーが発生します。水平グループには属していません。プログラムを実行すると、okButton になることがあり、次に openButton になり、次回はラジオボタンの 1 つになります。私もコードを変更しませんでした。私はドキュメントを読み、ParallelGroup水平グループのそれぞれに垂直グループが必要であり、SequentialGroupその逆も同様であることを理解しています。

コード:

package gui;

import javax.swing.ButtonGroup;
import javax.swing.GroupLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JProgressBar;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

import main.MainMenu;

import java.awt.Color;
import java.awt.Container;
import java.awt.Cursor;
import java.awt.ScrollPane;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

public class UserInterface 
{   
    JFrame guiFrame = new JFrame(); 
    MainMenu menu = new MainMenu();
    Task task;

    final JButton okButton = new JButton();

    public boolean done;
    public boolean isDrawed;
    public boolean okButtonIsPressed;
    public JScrollPane scrollPanel;
    public JProgressBar progressBar;
    public String text;
    public String outputPath = "C:\\Users\\Test\\";

    private JPanel mainPanel;   
    private JPanel dotPanel;       
    private ButtonGroup group;
    private String newFileName;   
    private JButton openButton;
    private JTextArea textForScrollPane;
    private JTextField textFieldFilePath;
    private JRadioButton radioButtonA;
    private JRadioButton radioButtonB;
    private JRadioButton radioButtonC;

    public void UserInterface()
    {
        task = new Task();
        dotPanel  = new JPanel();
        mainPanel  = new JPanel();
        group = new ButtonGroup();
        openButton = new JButton();
        progressBar = new JProgressBar();
        textFieldFilePath  = new JTextField();
        textForScrollPane = new JTextArea(text);
        radioButtonA  = new JRadioButton();
        radioButtonB  = new JRadioButton();
        radioButtonC  = new JRadioButton();
        scrollPanel  = new JScrollPane(textForScrollPane, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    }

    public void makeFrame()
    {       
        isDrawed = false;
        okButtonIsPressed = false;

        //Settings for the Frame
        guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        guiFrame.setSize(500,500);
        guiFrame.setLocationRelativeTo(null);
        guiFrame.setVisible(true);

        //Give the right names to the buttons and textfields
        okButton.setText("OK");
        openButton.setText("Open");
        textFieldFilePath.setText("...Choos path...");
        radioButtonA.setText("Option a");
        radioButtonB.setText("option b");
        radioButtonC.setText("option c");

        //Make a buttonGroup for the radio buttons, for easy use
        group.add(radioButtonA);
        group.add(radioButtonB);
        group.add(radioButtonC);

        //Set the textForScrollPane
        textForScrollPane.setLineWrap(true);
        textForScrollPane.setEditable(false);
        textForScrollPane.setWrapStyleWord(true);

        //Settings for the progressbar
        progressBar = new JProgressBar(0, 100);
        progressBar.setValue(50);
        progressBar.setForeground(Color.blue);
        progressBar.setStringPainted(true);
        isDrawed = true;
        System.out.println("~~~~~~drawed");

        makePanelLayout();
    }

    public void panelActions()
    {
        //Make a new thread to update the frame and make progress on the same time
        Runnable runner = new Runnable()
        {
            @Override
            public void run() 
            {
                System.out.println("running");

                okButton.setActionCommand("ok");
                okButton.addActionListener(new ActionListener()
                {
                    @Override
                    public void actionPerformed(ActionEvent evt) 
                    {
                        okButton.setEnabled(false);
                        Container container = guiFrame.getContentPane();
                        container.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
                        done = false;
                        okButtonIsPressed = true;
                   }
                });

                openButton.addActionListener(new ActionListener()
                {
                    @Override
                    public void actionPerformed(ActionEvent evt)
                    {
                        openButton.setEnabled(false);
                        String path = "C:\\Users\\";
                        String openPath = menu.ChooseFile(path);
                        textFieldFilePath.setText(openPath);                        
                    }
                });

                task.addPropertyChangeListener(new PropertyChangeListener()
                {
                    @Override
                    public void propertyChange(PropertyChangeEvent evt) 
                    {
                        if ("progress" == evt.getPropertyName()) 
                        {
                            int progress = (Integer) evt.getNewValue();
                            progressBar.setIndeterminate(false);
                            progressBar.setValue(progress);
                            System.out.println("update progress " + progress);
                            //scrollPanel.add(String.format("Completed %d%% of task.\n", progress), guiFrame);
                        }
                    }
                });
                task.execute();
            };
        };
        Thread tProgressbar = new Thread(runner, "Update progressbar");
        tProgressbar.start();
    }

    public void makePanelLayout()
    {
        GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 176, Short.MAX_VALUE));        
        mainPanelLayout.setVerticalGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 0, Short.MAX_VALUE));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(guiFrame.getContentPane());
        guiFrame.getContentPane().setLayout(layout);

        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(scrollPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 364, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addGap(106, 106, 106)
                                    .addComponent(okButton))
                                    .addComponent(radioButtonA)
                                .addComponent(radioButtonB)
                                .addComponent(radioButtonC))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(dotPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, 364, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(textFieldFilePath, javax.swing.GroupLayout.PREFERRED_SIZE, 287, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addComponent(openButton)))
                    .addContainerGap(39, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(22, 22, 22)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(dotPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(radioButtonA)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(radioButtonB)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(radioButtonC)))
                    .addGap(18, 18, 18)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(textFieldFilePath, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(openButton))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(okButton)
                    .addGap(18, 18, 18)
                    .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(scrollPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );
    }
}

__________________________
the other class:
__________________________
package gui;

import java.awt.Container;
import java.awt.Cursor;
import java.awt.Toolkit;
import java.util.List;
import java.util.Random;

import javax.swing.JLabel;
import javax.swing.SwingWorker;

class Task extends SwingWorker<Void, Integer> 
{
    public int progress = 0;
    UserInterface use = new UserInterface();

    @Override
    public Void doInBackground() {
        Random random = new Random();
        progress = 0;
        setProgress(0);

        //Sleep for at least one second to simulate "startup".
        try 
        {
            Thread.sleep(1000 + random.nextInt(2000));
        } 
        catch (InterruptedException ignore) {}

        if(use.isDrawed && use.okButtonIsPressed)
        {
            while (progress < 100) 
            {
                //Sleep for up to one second.
                try 
                {
                    Thread.sleep(random.nextInt(1000));
                } 
                catch (InterruptedException ignore) {}

                //Make random progress.
                progress += random.nextInt(10);
                System.out.println("progress" + progress);
                publish(progress);
                use.text = "progress " + progress;
                System.out.println("text updated");
                use.scrollPanel.setViewportView(new JLabel("progress " + progress));
                setProgress(Math.min(progress, 100));
            }
        }
        return null;
    }

    @Override
    protected void process(List<Integer> chunks) 
    {
        System.out.println("in the process");
        if(chunks != null && use.isDrawed)
        {
            for (int chunk : chunks) 
            {
                if(chunk > 0)
                {
                    use.progressBar.setValue(chunk);
                    System.out.println("chunk " + chunk);
                }
            }
        }
    }

    public void done() 
    {
        Toolkit.getDefaultToolkit().beep();
        use.okButton.setEnabled(true);        
        Container container = use.guiFrame.getContentPane();
        progress = 100;
        container.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
    }

    public int getLengthOfTask() 
    {
        return progress;
    }
}   

エラー:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: javax.swing.JButton[,0,0,0x0,invalid,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@4a53fb57,flags=296,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=javax.swing.plaf.InsetsUIResource[top=2,left=14,bottom=2,right=14],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=OK,defaultCapable=true] is not attached to a horizontal group
at javax.swing.GroupLayout.checkComponents(GroupLayout.java:1065)
at javax.swing.GroupLayout.prepare(GroupLayout.java:1019)
at javax.swing.GroupLayout.layoutContainer(GroupLayout.java:889)
at java.awt.Container.layout(Container.java:1419)
at java.awt.Container.doLayout(Container.java:1408)
at java.awt.Container.validateTree(Container.java:1505)
at java.awt.Container.validateTree(Container.java:1511)
at java.awt.Container.validateTree(Container.java:1511)
at java.awt.Container.validateTree(Container.java:1511)
at java.awt.Container.validate(Container.java:1478)
at java.awt.Window.dispatchEventImpl(Window.java:2482)
at java.awt.Component.dispatchEvent(Component.java:4488)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:674)
at java.awt.EventQueue.access$400(EventQueue.java:81)
at java.awt.EventQueue$2.run(EventQueue.java:633)
at java.awt.EventQueue$2.run(EventQueue.java:631)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
at java.awt.EventQueue$3.run(EventQueue.java:647)
at java.awt.EventQueue$3.run(EventQueue.java:645)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:644)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

助けてくれてありがとう :)

4

0 に答える 0