关于我之前的问题How to stop .next() and .previous() from looping似乎每当我取消/完成/关闭我的jframe时,我都无法让我的原始程序再次运行,就像我第一次运行它一样。 / p>
我在netbeans中创建了下面的代码,而我的原始代码是在eclipse中。在我运行程序的地方不应该有区别,对吗?但那时..我在eclipse中的原始程序中有这个代码,它只在我运行程序时执行.setEnable
和.setText
一次。即使两个平台都具有相同的代码,我也会获得不同的结果。这是自然的吗?我的下面的代码完全正常,但在我的原始代码中它不是。我在这里做错了吗?我应该从原始代码中发布一段代码进行比较吗?
这是我的示例代码,虽然不是那么短......
主要课程:
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
*
* @author lenovo
*/
public class main {
JFrame Card = new JFrame();
public main(){
Card.setVisible(true);
Card.setSize(605,333);
Card.setTitle("Tank Delivery");
Card.setResizable(false);
final Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
int x=(int)((dimension.getWidth() - Card.getWidth())/2);
int y=(int)((dimension.getHeight() - Card.getHeight())/2);
Card.setLocation(x, y);
Card.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel panel = new JPanel();
JButton a = new JButton("OPEN");
a.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
gridbaglayoutdemo g = new gridbaglayoutdemo();
}
});
panel.add(a);
Card.add(panel);
}
public static void main(String[] args){
//Use the event dispatch thread for Swing components
EventQueue.invokeLater(new Runnable()
{
@Override
public void run()
{
new main();
}
});
}
}
我的另一堂课:
package cardlayoutalignment;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.BevelBorder;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
public class gridbaglayoutdemo {
JFrame Card = new JFrame();
FlowLayout flow = new FlowLayout(FlowLayout.RIGHT,2,2);
Border etch = BorderFactory.createEtchedBorder(Color.white,Color.gray);
Border margin = new EmptyBorder(10,10,10,10);
public static GridBagLayout grid = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
final static boolean shouldFill = true;
JPanel container;
JPanel divider = new JPanel();
JPanel bodypanel = new JPanel();
final JPanel buttonpanel = new JPanel();
JPanel panel_1 = new JPanel();
JPanel panel_2 = new JPanel();
JPanel panel_3 = new JPanel();
CardLayout cl = new CardLayout();
JTextArea text_2;
JTextArea text_3;
String change = "Finish";
final JButton btnNext;
final JButton btnBack;
int currentCard = 0;
int cardflag = 0;
AbstractAction backAction;
AbstractAction nextAction;
public gridbaglayoutdemo(){
Card.setVisible(true);
Card.setSize(605,333);
Card.setTitle("Tank Delivery");
Card.setResizable(false);
final Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
int x=(int)((dimension.getWidth() - Card.getWidth())/2);
int y=(int)((dimension.getHeight() - Card.getHeight())/2);
Card.setLocation(x, y);
Card.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
bodypanel.setLayout(new BorderLayout());
divider.setLayout(new BorderLayout());
container = new JPanel(cl);
container.setLayout(cl);
cl.show(container, "1");
panel_1.setLayout(grid);
JLabel label_1 = new JLabel("Enter 1:");
label_1.setFont(new Font("Arial", Font.PLAIN, 18));
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.weighty = 0;
c.gridx = 0;
c.gridy = 0;
c.insets = new Insets(10,10,0,0);
panel_1.add(label_1, c);
JComboBox box_1 = new JComboBox();
box_1.setPreferredSize(new Dimension(200,30));
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.weighty = 0;
c.gridx = 0;
c.gridy = 1;
c.insets = new Insets(10,10,0,0);
panel_1.add(box_1,c);
JLabel label = new JLabel("");
label.setFont(new Font("Arial", Font.PLAIN, 18));
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.weighty = 1;
c.gridx = 0;
c.gridy = 2;
c.insets = new Insets(10,0,0,0);
panel_1.add(label, c);
panel_2.setLayout(grid);
JLabel label_2 = new JLabel("Enter 2:");
label_2.setFont(new Font("Arial", Font.PLAIN, 18));
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.weighty = 0;
c.gridx = 0;
c.gridy = 0;
c.insets = new Insets(10,10,0,0);
panel_2.add(label_2,c);
text_2 = new JTextArea();
text_2.setPreferredSize(new Dimension(200,30));
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.weighty = 0;
c.gridx = 0;
c.gridy = 20;
c.insets = new Insets(10,10,0,0);
panel_2.add(text_2,c);
JLabel label_22 = new JLabel("");
label_22.setFont(new Font("Arial", Font.PLAIN, 18));
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.weighty = 1;
c.gridx = 0;
c.gridy = 30;
c.insets = new Insets(10,0,0,0);
panel_2.add(label_22, c);
panel_3.setLayout(grid);
JLabel label_3 = new JLabel("Enter 3:");
label_3.setFont(new Font("Arial", Font.PLAIN, 18));
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.weighty = 0;
c.gridx = 0;
c.gridy = 0;
c.insets = new Insets(10,10,0,0);
panel_3.add(label_3,c);
text_3 = new JTextArea();
text_3.setPreferredSize(new Dimension(200,30));
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.weighty = 0;
c.gridx = 0;
c.gridy = 20;
c.insets = new Insets(10,10,0,0);
panel_3.add(text_3,c);
JLabel label_33 = new JLabel("");
label_33.setFont(new Font("Arial", Font.PLAIN, 18));
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.weighty = 1;
c.gridx = 0;
c.gridy = 30;
c.insets = new Insets(10,0,0,0);
panel_3.add(label_33, c);
buttonpanel.setLayout(new FlowLayout(SwingConstants.RIGHT));
buttonpanel.setBorder(new EmptyBorder(0,10,0,0));
buttonpanel.setLayout(new FlowLayout(SwingConstants.RIGHT));
buttonpanel.setBorder(new EmptyBorder(0,0,0,0));
btnBack = new JButton("< Back");
btnBack.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cl.previous(container);
buttonpanel.repaint();
cardflag--;
if (cardflag==0)
{btnBack.setEnabled(false);}
if(cardflag<3)
{btnNext.setText("Next >");}
}
});
btnBack.setEnabled(false);
btnBack.setFont(new Font("Arial", Font.PLAIN, 20));
btnBack.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
btnBack.setPreferredSize(new Dimension(110, 40));
btnBack.setBackground(new Color(224,223,227));
buttonpanel.add(btnBack);
btnNext = new JButton("Next >");
btnNext.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cl.next(container);
buttonpanel.repaint();
cardflag++;
if(cardflag<3)
{btnBack.setEnabled(true);}
if(cardflag==2)
{btnNext.setText(change);}
if (cardflag==3)
{cl.show(container, "3");
JOptionPane.showMessageDialog(null, "DONE");
Window dialog = SwingUtilities.windowForComponent( btnNext );
dialog.dispose();
cardflag=0;
btnNext.setText("Next >");
}
validateText();
}
});
btnNext.setFont(new Font("Arial", Font.PLAIN, 20));
btnNext.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
btnNext.setPreferredSize(new Dimension(110, 40));
btnNext.setBackground(new Color(224,223,227));
btnNext.setVisible(true);
buttonpanel.add(btnNext);
final JButton btnCancel = new JButton("Cancel");
btnCancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cardflag=0;
Window dialog = SwingUtilities.windowForComponent( btnCancel );
dialog.dispose();
}
});
btnCancel.setFont(new Font("Arial", Font.PLAIN, 20));
btnCancel.setFocusable(false);
btnCancel.setFocusTraversalKeysEnabled(false);
btnCancel.setFocusPainted(false);
btnCancel.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
btnCancel.setPreferredSize(new Dimension(110, 40));
btnCancel.setBackground(new Color(224,223,227));
buttonpanel.add(btnCancel);
JPanel numberpanel = new JPanel();
numberpanel.setPreferredSize(new Dimension(221,0));
numberpanel.setBorder(new EmptyBorder(10,0,0,10));
numberpanel.setBorder(BorderFactory.createEtchedBorder(Color.white,Color.gray));
numberpanel.setLayout(flow);
JButton button_7 = new JButton("7");
button_7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
buttonaction(e);
}
});
button_7.setActionCommand("7");
button_7.setFont(new Font("Arial", Font.PLAIN, 30));
button_7.setFocusable(false);
button_7.setFocusTraversalKeysEnabled(false);
button_7.setFocusPainted(false);
button_7.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
button_7.setPreferredSize(new Dimension(70, 70));
button_7.setBackground(new Color(224,223,227));
numberpanel.add(button_7);
JButton button_8 = new JButton("8");
button_8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
buttonaction(e);
}
});
button_8.setActionCommand("8");
button_8.setFont(new Font("Arial", Font.PLAIN, 30));
button_8.setFocusable(false);
button_8.setFocusTraversalKeysEnabled(false);
button_8.setFocusPainted(false);
button_8.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
button_8.setPreferredSize(new Dimension(70, 70));
button_8.setBackground(new Color(224,223,227));
numberpanel.add(button_8);
JButton button_9 = new JButton("9");
button_9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
buttonaction(e);
}
});
button_9.setActionCommand("9");
button_9.setFont(new Font("Arial", Font.PLAIN, 30));
button_9.setFocusable(false);
button_9.setFocusTraversalKeysEnabled(false);
button_9.setFocusPainted(false);
button_9.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
button_9.setPreferredSize(new Dimension(70, 70));
button_9.setBackground(new Color(224,223,227));
numberpanel.add(button_9);
Card.add(bodypanel);
bodypanel.add(divider, BorderLayout.WEST);
divider.add(container, BorderLayout.NORTH);
container.add(panel_1, "1");
container.add(panel_2, "2");
container.add(panel_3, "3");
divider.add(buttonpanel, BorderLayout.SOUTH);
bodypanel.add(numberpanel, BorderLayout.EAST);
}
private void buttonaction(ActionEvent e){
try{
if(cardflag==1)
{text_2.append("" + e.getActionCommand());}
if(cardflag==2)
{text_3.append("" + e.getActionCommand());}
}catch(Exception e1){}
}
private void validateText(){
if(cardflag==2)
{String text = text_2.getText();
if (text.isEmpty()==true)
{JOptionPane.showMessageDialog(null, "Text 2 is empty!");
cl.show(container, "2");
btnNext.setText("Next >");
cardflag--;
}
}
}
}