我在这里有一个程序示例。我有3个面板,如果要插入面板,我想让用户停止按上一个按钮,最后一个面板panel_1
也要停止panel_3
。如果用户位于面板的开始或面板的末尾,有什么方法喜欢禁用按钮?
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 = new JPanel();
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();
public gridbaglayoutdemo(){
Card.setVisible(true);
Card.setSize(605,333);
Card.setTitle("");
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.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);
JTextField text_2 = new JTextField();
text_2.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_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 = 2;
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);
JTextField text_3 = new JTextField();
text_3.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_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 = 2;
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));
JButton btnBack = new JButton("< Back");
btnBack.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cl.previous(container);
buttonpanel.repaint();
}
});
btnBack.setFont(new Font("Arial", Font.PLAIN, 20));
btnBack.setFocusable(false);
btnBack.setFocusTraversalKeysEnabled(false);
btnBack.setFocusPainted(false);
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);
JButton btnNext = new JButton("Next >");
btnNext.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cl.next(container);
buttonpanel.repaint();
}
});
btnNext.setFont(new Font("Arial", Font.PLAIN, 20));
btnNext.setFocusable(false);
btnNext.setFocusTraversalKeysEnabled(false);
btnNext.setFocusPainted(false);
btnNext.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
btnNext.setPreferredSize(new Dimension(110, 40));
btnNext.setBackground(new Color(224,223,227));
buttonpanel.add(btnNext);
final JButton btnCancel = new JButton("Cancel");
btnCancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
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) {
}
});
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) {
}
});
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) {
}
});
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");
//container.add(panel_4, "4");
//container.add(p5.panel_5, "5");
//container.add(p6.panel_6, "6");
divider.add(buttonpanel, BorderLayout.SOUTH);
bodypanel.add(numberpanel, BorderLayout.EAST);
}
public static void main(String[] args){
//Use the event dispatch thread for Swing components
EventQueue.invokeLater(new Runnable()
{
@Override
public void run()
{
new gridbaglayoutdemo();
}
});
}
}
抱歉,工作代码不够短。我已经修剪了一些。那是我能做的最短的版本。
“如果用户是在面板的开始或面板的末尾,有没有办法喜欢禁用按钮?”
根据如何使用CardLayout和CardLayout
API
,没有直接的方法可以做到这一点。
但是您可以轻松实现此功能,使int变量与当前卡号保持一致,并再次检查其值ts 0(对于第一张卡)或容器的组件数(对于最后一张卡)。例如:
public class GridBagLayoutDemo { // note code convention!
int currentCard = 0;
Action backAction, nextAction;
...
public GridBagLayoutDemo() {
...
backAction = new AbstractAction("< Back") {
@Override
public void actionPerformed(ActionEvent e) {
currentCard--;
GridBagLayoutDemo.this.evaluateActions();
}
};
nextAction = new AbstractAction("Next >") {
@Override
public void actionPerformed(ActionEvent e) {
currentCard++;
GridBagLayoutDemo.this.evaluateActions();
}
};
JButton btnBack = new JButton(backAction);
...
JButton btnNext = new JButton(nextAction);
...
}
private void evaluateActions() {
backAction.setEnabled(currentCard > 0);
nextAction.setEnabled(currentCard < container.getComponentCount() - 1);
}
...
}
仔细研究CardLayout
实现,默认情况下实现此功能真的很容易(除非我遗漏了一些东西):
public class CardLayout implements LayoutManager2,
Serializable {
/*
* This creates a Vector to store associated
* pairs of components and their names.
* @see java.util.Vector
*/
Vector vector = new Vector();
/*
* Index of Component currently displayed by CardLayout.
*/
int currentCard = 0;
...
/*
* Hypothetical implementations
*/
public boolean isDisplayingFirstCard() {
return currentCard == 0;
}
public boolean isDisplayingLastCard() {
return currentCard == vector.size() - 1;
}
}
不知道为什么他们没有提供这种有用的功能。
问题内容: 我对Go如何处理非阻塞IO感到困惑。API在我看来基本上是同步的,并且在Go上观看演示时,听到诸如“和调用块”之类的注释并不罕见。 从文件或网络读取时,Go是否使用阻塞IO?还是当在Go Routine中使用某种魔术来重写代码? 来自C#背景,这感觉非常不直观,在C#中,当使用异步API时我们使用了关键字。这清楚地表明,API可以产生当前线程,并在以后的延续中继续。 因此,TLDR;当
问题内容: 我正在使用Javascript解析具有大约3500个元素的XML文件。我正在使用jQuery“每个”函数,但可以使用任何形式的循环。 问题是循环执行时浏览器冻结了几秒钟。停止冻结浏览器而不减慢代码速度的最佳方法是什么? 问题答案: 我会放弃“每个”功能而赞成使用for循环,因为它更快。我还将使用“ setTimeout”添加一些等待,但仅在需要时才如此。您不想每次等待5毫秒,因为处理3
问题内容: Ajax使用回调,因为它是同步的。 我希望对远程URL块的调用直到出现一些答案为止 ,就像在Ajax中一样,但是没有异步部分,或者我要说要进行JAX调用。 是否有任何技术可以使以下事情发生(使用JQuery)(…使用JQuery或其他解决方案): 我只是想知道-想学习。 实际上,有时会阻塞直到回复合适为止。我并不是说要浏览器阻止,而只是脚本运行时。 问题答案: 您可以在使用jQuery
我正在制作一个时间和日期的东西,我需要自动更新,所以我使用了一个while循环。不幸的是,它一直使模拟器崩溃。我认为这是由于堆栈溢出,但我不确定。我尝试在每个循环之间添加一个小延迟(大约1秒),但它仍然不起作用。我能做些什么来继续更新时间/日期而不使应用程序崩溃? 以下是while循环代码:
描述 (Description) 它确定已更改属性的先前值。 语法 (Syntax) model.previous(attribute) 参数 (Parameters) attribute - 它表示模型的属性。 例子 (Example) <!DOCTYPE html> <html> <head> <title>Model Example</title> <scri
语法 (Syntax) String previous() 参数 (Parameters) 没有 返回值 (Return Value) 此方法返回结果String。 例子 (Example) 以下是此方法的使用示例 - class Example { static void main(String[] args) { String a = "Hello"; pr