Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > Java: GUI и Java FX приложения > Перерисовка(почему не работает?)


Автор: acvilon 14.9.2007, 20:57
Код

public class TestMain {
    public static void main(String[] args) {
        TestFrame frame = new TestFrame();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
public class TestFrame extends JFrame {
    private static int frameMaxSizeX,
            frameMaxSizeY;
    public TestFrame(){
        setTitle("Super Test :)");
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        frameMaxSizeX = (int) toolkit.getScreenSize().getWidth();
        frameMaxSizeY = (int) toolkit.getScreenSize().getHeight();

        setBounds(frameMaxSizeX / 4, frameMaxSizeY / 4,
                frameMaxSizeX / 2, frameMaxSizeY / 2);

        TextPanel textPanel = new TextPanel();
        ButtonPanel buttonPanel = new ButtonPanel();
        buttonPanel.textPanel = textPanel;
        buttonPanel.testFrame = this;
        
        add(textPanel, BorderLayout.CENTER);
        add(buttonPanel, BorderLayout.SOUTH);

        }
    public static int getFrameMaxSizeX() {
        return frameMaxSizeX;
    }

    public static int getFrameMaxSizeY() {
        return frameMaxSizeY;
    }
}
public class TextPanel extends JPanel {
    public String status;

    private JLabel welcome;


    public TextPanel() {

        addWelcome("WELCOME");
    }

    public void addWelcome(String test) {
        status = Constants.WELCOME;
        welcome = new JLabel("<html><h1>" + test + "</h1></html>");
        welcome.setVisible(true);
        add(welcome, BorderLayout.CENTER);
    }

    public void hideWelcome() {
        welcome.setVisible(false);
        repaint();
    }
}
public class ButtonPanel extends JPanel {
    public TextPanel textPanel;
    public TestFrame testFrame;
    public ButtonPanel() {
        JButton next = new JButton("Next");
        next.setPreferredSize(new Dimension(70, 20));
        next.setMaximumSize(new Dimension(70, 20));
        next.setMinimumSize(new Dimension(70, 20));
        next.setFocusable(false);
        next.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                if(textPanel.status.equals(Constants.WELCOME)) {
                      textPanel.hideWelcome();
                      textPanel.status= "1";
                }else{
                    textPanel.addWelcome("blablablabla");
                }
            }
        });
        JButton exit = new JButton("Exit");
        exit.setPreferredSize(new Dimension(70, 20));
        exit.setMaximumSize(new Dimension(70, 20));
        exit.setMinimumSize(new Dimension(70, 20));
        exit.setFocusable(false);
        add(next, BorderLayout.WEST);
        add(exit, BorderLayout.WEST);
    }
}
public class Constants {
    public static String WELCOME = "Welcome";
    public static String INTRODUCTION= "Welcome";    
}

Это часть програмы и она не работает. Эта часть должна при первом нажатии на кнопку next скрыть надпись Welcome, при втором показать надпись blablabla. Но она при првом нажатии скрывает надпись, при втором ничего не делает, но если после второго взять изменить размер экна, то при перерисовке он выведет вторую надпись.
Подскажите как исправить?!

Автор: dorogoyIV 14.9.2007, 22:43
74 строка
Код

else
                {
                    textPanel.addWelcome("blablablabla");
                    textPanel.getLayout().layoutContainer(textPanel);
                    repaint();
                }

Автор: acvilon 15.9.2007, 00:54
Спасибо, помогло.

Автор: acvilon 15.9.2007, 11:33
Доработав еще этот кусок, у меня опять возникла похожая проблема, объясните в чем проблема, и как сделать чтоб следующее работало:
Код

public class TestMain {
    public static void main(String[] args) {
        TestFrame frame = new TestFrame();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
public class TestFrame extends JFrame {
    private static int frameMaxSizeX,
            frameMaxSizeY;
    public TestFrame(){
        setTitle("Super Test :)");
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        frameMaxSizeX = (int) toolkit.getScreenSize().getWidth();
        frameMaxSizeY = (int) toolkit.getScreenSize().getHeight();

        setBounds(frameMaxSizeX / 4, frameMaxSizeY / 4,
                frameMaxSizeX / 2, frameMaxSizeY / 2);

        TextPanel textPanel = new TextPanel();
        ButtonPanel buttonPanel = new ButtonPanel();
        buttonPanel.textPanel = textPanel;
        buttonPanel.testFrame = this;
        
        add(textPanel, BorderLayout.CENTER);
        add(buttonPanel, BorderLayout.SOUTH);

        }
    public static int getFrameMaxSizeX() {
        return frameMaxSizeX;
    }

    public static int getFrameMaxSizeY() {
        return frameMaxSizeY;
    }
}
public class TextPanel extends JPanel {
    public String status;
    private JTextField fieldName,
            fieldSurname,
            fieldPatronymic;

    private JLabel welcome = new JLabel();
    private JPanel panelName = new JPanel(),
            panelSurname = new JPanel(),
            panelPatronymic = new JPanel();



    public JTextField getFieldName() {
        return fieldName;
    }

    public JTextField getFieldSurname() {
        return fieldSurname;
    }

    public JTextField getFieldPatronymic() {
        return fieldPatronymic;
    }

    public TextPanel() {

        addWelcome("WELCOME");
    }

    public void addFIO() {
        fieldName = new JTextField("");
        fieldName.setFocusable(true);
        fieldName.setPreferredSize(new Dimension(200, 20));
        fieldName.setMaximumSize(new Dimension(200, 20));
        fieldName.setMinimumSize(new Dimension(200, 20));

        JLabel labelName = new JLabel("Name:           ");
        panelName.setVisible(true);
        panelName.add(labelName, BorderLayout.CENTER);
        panelName.add(fieldName, BorderLayout.CENTER);
        panelName.setPreferredSize(new Dimension(TestFrame.getFrameMaxSizeX(), 30));

        fieldSurname = new JTextField("");
        fieldSurname.setFocusable(true);
        fieldSurname.setPreferredSize(new Dimension(200, 20));
        fieldSurname.setMaximumSize(new Dimension(200, 20));
        fieldSurname.setMinimumSize(new Dimension(200, 20));

        JLabel labelSurname = new JLabel("Surname:    ");
        panelSurname.setVisible(true);
        panelSurname.add(labelSurname, BorderLayout.CENTER);
        panelSurname.add(fieldSurname, BorderLayout.CENTER);
        panelSurname.setPreferredSize(new Dimension(TestFrame.getFrameMaxSizeX(), 30));

        fieldPatronymic = new JTextField("");
        fieldPatronymic.setFocusable(true);
        fieldPatronymic.setPreferredSize(new Dimension(200, 20));
        fieldPatronymic.setMaximumSize(new Dimension(200, 20));
        fieldPatronymic.setMinimumSize(new Dimension(200, 20));

        JLabel labelPatronymic = new JLabel("Patronymic:");
        panelPatronymic.setVisible(true);
        panelPatronymic.add(labelPatronymic, BorderLayout.CENTER);
        panelPatronymic.add(fieldPatronymic, BorderLayout.CENTER);
        panelPatronymic.setPreferredSize(new Dimension(TestFrame.getFrameMaxSizeX(), 30));


        JLabel intro = new JLabel("Hi, please enter your information :)");

        add(intro, BorderLayout.CENTER);
        add(panelName, BorderLayout.CENTER);
        add(panelSurname, BorderLayout.CENTER);
        add(panelPatronymic, BorderLayout.CENTER);
        getLayout().layoutContainer(this);
        repaint();
    }

    public void hideFIO(){
        panelName.setVisible(false);
        panelSurname.setVisible(false);
        panelPatronymic.setVisible(false);
    }

    public void addWelcome(String test) {
        status = Constants.WELCOME;
        welcome.setText("<html><h1>" + test + "</h1></html>");
        welcome.setVisible(true);
        add(welcome, BorderLayout.CENTER);
        getLayout().layoutContainer(this);
        repaint();
    }

    public void hideWelcome() {
        welcome.setVisible(false);
    }
}
public class ButtonPanel extends JPanel {
    public TextPanel textPanel;
    public TestFrame testFrame;

    public ButtonPanel() {
        JButton next = new JButton("Next");
        next.setPreferredSize(new Dimension(70, 20));
        next.setMaximumSize(new Dimension(70, 20));
        next.setMinimumSize(new Dimension(70, 20));
        next.setFocusable(false);
        next.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (textPanel.status.equals(Constants.WELCOME)) {
                    textPanel.hideWelcome();
                    textPanel.status = "1";
                } else {
                    textPanel.addFIO();
                }
            }
        });
        JButton exit = new JButton("Exit");
        exit.setPreferredSize(new Dimension(70, 20));
        exit.setMaximumSize(new Dimension(70, 20));
        exit.setMinimumSize(new Dimension(70, 20));
        exit.setFocusable(false);
        add(next, BorderLayout.WEST);
        add(exit, BorderLayout.WEST);
    }
}
public class Constants {
    public static String WELCOME = "Welcome";
    public static String INTRODUCTION= "Welcome";    
}


Сейчас просто добавлено, что после первого нажатия исчезает надпись велкам, после второго появляются Labels с текством и поля ввода текста, поля ввода не появляются...
Заранее спасибо

Автор: dorogoyIV 15.9.2007, 15:01
 smile ну ты панелей нагородил
так работает:
Код

textPanel.panelName.getLayout().layoutContainer(textPanel.panelName);
repaint();


но пришлось сделать еще так:
Код

   /* private*/ JPanel panelName = new JPanel(),
            panelSurname = new JPanel(),
            panelPatronymic = new JPanel();


Автор: acvilon 15.9.2007, 15:19
Спасибо, но ты не мог бы объяснить в чем причина?

Автор: dorogoyIV 15.9.2007, 15:42
грамотно не смогу  smile 
в общем перерисовка это  smile 
может кто другой объяснит?

Автор: acvilon 15.9.2007, 17:15
Почему просто тогда не достаточно функции repaint() ?

Автор: dorogoyIV 15.9.2007, 17:21
по всей видимости репейнт или валидейт перерисовывает только фрейм, а панели остаются как были. но это только предположение.

о! так эти методы (репейнт, валидейт) лайот менеджер глушит. наверное?

Автор: acvilon 16.9.2007, 14:00
Странно, но почему то у меня не работает? Ты точно проверял что работает?

Автор: dorogoyIV 16.9.2007, 14:10
вот так у меня работает
Код

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

public class TestMain {
    public static void main(String[] args) {
        TestFrame frame = new TestFrame();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
/*public*/ class TestFrame extends JFrame {
    private static int frameMaxSizeX,
            frameMaxSizeY;
    public TestFrame(){
        setTitle("Super Test :)");
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        frameMaxSizeX = (int) toolkit.getScreenSize().getWidth();
        frameMaxSizeY = (int) toolkit.getScreenSize().getHeight();

        setBounds(frameMaxSizeX / 4, frameMaxSizeY / 4,
                frameMaxSizeX / 2, frameMaxSizeY / 2);

        TextPanel textPanel = new TextPanel();
        ButtonPanel buttonPanel = new ButtonPanel();
        buttonPanel.textPanel = textPanel;
        buttonPanel.testFrame = this;
        
        add(textPanel, BorderLayout.CENTER);
        add(buttonPanel, BorderLayout.SOUTH);

        }
    public static int getFrameMaxSizeX() {
        return frameMaxSizeX;
    }

    public static int getFrameMaxSizeY() {
        return frameMaxSizeY;
    }
}
/*public*/ class TextPanel extends JPanel {
    public String status;
    private JTextField fieldName,
            fieldSurname,
            fieldPatronymic;

    private JLabel welcome = new JLabel();
    /*private*/ JPanel panelName = new JPanel(),
            panelSurname = new JPanel(),
            panelPatronymic = new JPanel();



    public JTextField getFieldName() {
        return fieldName;
    }

    public JTextField getFieldSurname() {
        return fieldSurname;
    }

    public JTextField getFieldPatronymic() {
        return fieldPatronymic;
    }

    public TextPanel() {

        addWelcome("WELCOME");
    }

    public void addFIO() {
        fieldName = new JTextField("");
        fieldName.setFocusable(true);
        fieldName.setPreferredSize(new Dimension(200, 20));
        fieldName.setMaximumSize(new Dimension(200, 20));
        fieldName.setMinimumSize(new Dimension(200, 20));

        JLabel labelName = new JLabel("Name:           ");
        panelName.setVisible(true);
        panelName.add(labelName, BorderLayout.CENTER);
        panelName.add(fieldName, BorderLayout.CENTER);
        panelName.setPreferredSize(new Dimension(TestFrame.getFrameMaxSizeX(), 30));

        fieldSurname = new JTextField("");
        fieldSurname.setFocusable(true);
        fieldSurname.setPreferredSize(new Dimension(200, 20));
        fieldSurname.setMaximumSize(new Dimension(200, 20));
        fieldSurname.setMinimumSize(new Dimension(200, 20));

        JLabel labelSurname = new JLabel("Surname:    ");
        panelSurname.setVisible(true);
        panelSurname.add(labelSurname, BorderLayout.CENTER);
        panelSurname.add(fieldSurname, BorderLayout.CENTER);
        panelSurname.setPreferredSize(new Dimension(TestFrame.getFrameMaxSizeX(), 30));

        fieldPatronymic = new JTextField("");
        fieldPatronymic.setFocusable(true);
        fieldPatronymic.setPreferredSize(new Dimension(200, 20));
        fieldPatronymic.setMaximumSize(new Dimension(200, 20));
        fieldPatronymic.setMinimumSize(new Dimension(200, 20));

        JLabel labelPatronymic = new JLabel("Patronymic:");
        panelPatronymic.setVisible(true);
        panelPatronymic.add(labelPatronymic, BorderLayout.CENTER);
        panelPatronymic.add(fieldPatronymic, BorderLayout.CENTER);
        panelPatronymic.setPreferredSize(new Dimension(TestFrame.getFrameMaxSizeX(), 30));


        JLabel intro = new JLabel("Hi, please enter your information :)");

        add(intro, BorderLayout.CENTER);
        add(panelName, BorderLayout.CENTER);
        add(panelSurname, BorderLayout.CENTER);
        add(panelPatronymic, BorderLayout.CENTER);
        getLayout().layoutContainer(this);
        repaint();
    }

    public void hideFIO(){
        panelName.setVisible(false);
        panelSurname.setVisible(false);
        panelPatronymic.setVisible(false);
    }

    public void addWelcome(String test) {
        status = Constants.WELCOME;
        welcome.setText("<html><h1>" + test + "</h1></html>");
        welcome.setVisible(true);
        add(welcome, BorderLayout.CENTER);
        getLayout().layoutContainer(this);
        repaint();
    }

    public void hideWelcome() {
        welcome.setVisible(false);
    }
}
/*public*/ class ButtonPanel extends JPanel {
    public TextPanel textPanel;
    public TestFrame testFrame;

    public ButtonPanel() {
        JButton next = new JButton("Next");
        next.setPreferredSize(new Dimension(70, 20));
        next.setMaximumSize(new Dimension(70, 20));
        next.setMinimumSize(new Dimension(70, 20));
        next.setFocusable(false);
        next.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (textPanel.status.equals(Constants.WELCOME)) {
                    textPanel.hideWelcome();
                    textPanel.status = "1";
                } else {
                    textPanel.addFIO();

                    textPanel.panelName.getLayout().layoutContainer(textPanel.panelName);
                    textPanel.panelSurname.getLayout().layoutContainer(textPanel.panelSurname);
                    textPanel.panelPatronymic.getLayout().layoutContainer(textPanel.panelPatronymic);
                    repaint();
                }
            }
        });
        JButton exit = new JButton("Exit");
        exit.setPreferredSize(new Dimension(70, 20));
        exit.setMaximumSize(new Dimension(70, 20));
        exit.setMinimumSize(new Dimension(70, 20));
        exit.setFocusable(false);
        add(next, BorderLayout.WEST);
        add(exit, BorderLayout.WEST);
    }
}
/*public*/ class Constants {
    public static String WELCOME = "Welcome";
    public static String INTRODUCTION= "Welcome";    

}

Автор: acvilon 16.9.2007, 14:24
Вот, спасибо...

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)