import javax.swing.*;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
public class Gui extends JFrame {
JPanel panel1;
JMenuBar daysMenu;
JMenu[] days;
JMenu chestMenu, backMenu, legsMenu, armsMenu, shouldersMenu;
JMenuItem[] chest, back, legs, arms, shoulders;
JLabel[] abs1, abs2;
public Gui() {
setResizable(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// setSize(1000,1000);
setTitle("Gym Workout Program");
setLocationRelativeTo(null);
days = new JMenu[7];
for (int i = 0; i < days.length; i++) {
days[i] = new JMenu();
}
days[0].setText("MONDAY");
days[1].setText("TUESDAY");
days[2].setText("WEDNESDAY");
days[3].setText("THURSDAY");
days[4].setText("FRIDAY");
days[5].setText("SATURDAY");
days[6].setText("SUNDAY");
daysMenu = new JMenuBar();
for (JMenu day : days) {
daysMenu.add(day);
}
setJMenuBar(daysMenu);
// bodyPartsMenu
chestMenu = new JMenu("Chest(1X12,2X10,2X8)");
chest = new JMenuItem[5];
for (int i = 0; i < chest.length; i++) {
chest[i] = new JMenuItem();
}
chest[0].setText("Bench Press");
chest[1].setText("Incline Press w/ Dumbbells");
chest[2].setText("Chest Flyes w/ Dumbbells 180˚");
chest[3].setText("Pullover w/ Dumbbells");
chest[4].setText("Dips(Reps till failure/3X10)");
for (JMenuItem item : chest) {
chestMenu.add(item);
}
days[0].add(chestMenu);
backMenu = new JMenu("Back(1X12,2X10,2X8)");
back = new JMenuItem[4];
for (int i = 0; i < back.length; i++) {
back[i] = new JMenuItem();
}
back[0].setText("Pullups 3X10 TRYHARD/ Κ.Λ. Front Pulldowns");
back[1].setText("Bend Over Row with Bar");
back[2].setText("Lat Pulldowns");
back[3].setText("Rowing Machine");
for (JMenuItem item : back) {
backMenu.add(item);
}
days[1].add(backMenu);
days[2].add("KENO");
legsMenu = new JMenu("Legs (1X12,2X10,2X8)");
legs = new JMenuItem[5];
for (int i = 0; i < legs.length; i++) {
legs[i] = new JMenuItem();
}
legs[0].setText("Body Squat/Smith Machine/ w/Dumbbells");
legs[1].setText("Leg Extensions Machine");
legs[2].setText("Leg Curls Machine");
legs[3].setText("Leg Press/ Walking Lunges w/ Dumbbells");
legs[4].setText("Calves Machine 4X15");
for (JMenuItem item : legs) {
legsMenu.add(item);
}
days[3].add(legsMenu);
armsMenu = new JMenu("Arms (1X12,2X10,2X8)");
arms = new JMenuItem[6];
for (int i = 0; i < arms.length; i++) {
arms[i] = new JMenuItem();
}
arms[0].setText("Dumbbell Curls w/ twist");
arms[1].setText("Larry Scot");
arms[2].setText("Dumbbell Hammer Curls");
arms[3].setText("SkullCrushers w/ Bar/Dumbbells");
arms[4].setText("Triceps Press Downs");
arms[5].setText("Reverse Triceps Press Downs");
int countSeperator = 0;
for (JMenuItem item : arms) {
countSeperator++;
armsMenu.add(item);
if (countSeperator == 3) {
armsMenu.addSeparator();
}
}
days[4].add(armsMenu);
shouldersMenu = new JMenu("Shoulders(1X12,2X10,2X8)");
shoulders = new JMenuItem[4];
for (int i = 0; i < shoulders.length; i++) {
shoulders[i] = new JMenuItem();
}
shoulders[0].setText("Military Press Smith Machine");
shoulders[1].setText("Lateral Raises");
shoulders[2].setText("Shoulder Press Machine");
shoulders[3].setText("Traps");
for (JMenuItem item : shoulders) {
shouldersMenu.add(item);
}
days[5].add(shouldersMenu);
days[6].add("KENO");
abs1 = new JLabel[5];
for (int i = 0; i < abs1.length; i++) {
abs1[i] = new JLabel();
}
abs1[0].setText("Full and Elbow Plank(1min each)");
abs1[1].setText("Both Sides Plank(35-50 sec)");
abs1[2].setText("Knee Up-LsitHold High Bar(1min each)");
abs1[3].setText("HollowBodyHold(2X1min) LowerBackDown");
abs1[4].setText("Wheel(2X8reps)");
abs2 = new JLabel[3];
for (int i = 0; i < abs2.length; i++) {
abs2[i] = new JLabel();
}
abs2[0].setText("Leg Raises(3X12)");
abs2[1].setText("Knee Raises(3X12)");
abs2[2].setText("Side and Obliques(3X12)");
JLabel abs3 = new JLabel("Free Choice");
JLabel abs = new JLabel("Abs(3/3+ Times/week)");
add(abs, BorderLayout.NORTH);
JRadioButton b1 = new JRadioButton("Style 1");
JRadioButton b2 = new JRadioButton("Style 2");
JRadioButton b3 = new JRadioButton("Style 3");
ButtonGroup group = new ButtonGroup();
group.add(b1);
group.add(b2);
group.add(b3);
JPanel panel2 = new JPanel(new GridLayout(0, 1));
b1.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
for (JLabel label : abs1) {
panel2.add(label);
}
}
if (e.getStateChange() == ItemEvent.DESELECTED) {
for (JLabel label : abs1) {
panel2.remove(label);
}
}
}
});
b2.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
for (JLabel label : abs2) {
panel2.add(label);
}
}
if (e.getStateChange() == ItemEvent.DESELECTED) {
for (JLabel label : abs2) {
panel2.remove(label);
}
}
}
});
b3.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
panel2.add(abs3);
}
if (e.getStateChange() == ItemEvent.DESELECTED) {
panel2.remove(abs3);
}
}
});
add(panel2, BorderLayout.LINE_END);
panel1 = new JPanel(new GridLayout(0, 1, 50, 50));
panel1.add(b1);
panel1.add(b2);
panel1.add(b3);
add(panel1, BorderLayout.LINE_START);
pack();
setVisible(true);
}
}
我正在尝试使用JLabels和JRadioButtons显示我的“abs workout”程序。
我在添加所有组件后使用了setVisible
和setVisible(true)
之前的包,但除非我调整窗口大小,否则我的项目(abs1或abs2或abs3)将不会显示。
当我点击样式1时,没有任何反应,然后如果我调整窗口大小,一切似乎都正常。 有什么帮助吗?
答案 0 :(得分:0)
更改容器中的组件后,需要调用revalidate()来安排容器的重新布局。
答案 1 :(得分:0)
在面板中添加和删除标签后,您需要调用revalidate()
方法。你的代码看起来应该是这样的。
if(e.getStateChange()==ItemEvent.SELECTED){
for(JLabel label: abs1){
panel2.add(label);
revalidate();
}
}
if(e.getStateChange()==ItemEvent.DESELECTED){
for(JLabel label: abs1){
panel2.remove(label);
revalidate();
}
}
编辑:答案类似于评论中提到的内容,以及之前的答案:/