patternjavaMinor
JFrame with four sliders
Viewed 0 times
withslidersjframefour
Problem
I am writing a control panel with Sliders but I think my code is messy and cumbersome, what can I do to improve it?
public class Input_Window extends JFrame {
JSlider myslideA,myslideB,myslideC,myslideD;
Input_Window(){
System.out.println("* Control Window");
setBackground(Color.GRAY);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Controls");
setSize(200,400);
setLocation(1200,250);
setLayout(new FlowLayout());
myslideA=slider(0,10,3,1,true,true);
add(myslideA);
myslideB=slider(0,10,3,1,true,true);
add(myslideB);
myslideC=slider(0,10,3,1,true,true);
add(myslideC);
myslideD=slider(0,10,3,1,true,true);
add(myslideD);
setVisible(true);
setFocusable(true);
requestFocus();
}
public final JSlider slider(int min, int max, int def, int maj, boolean ticks, boolean labels){
JSlider myslide=new JSlider(JSlider.HORIZONTAL,min,max,def);
myslide.setMajorTickSpacing(maj);
myslide.setPaintTicks(ticks);
myslide.setPaintLabels(labels);
return (myslide);
}
}Solution
-
Naming:
Your class is named
-
Formatting:
An equal sign should be surrounded by spaces:
-
Visibilities:
your InputWindow should have to know about them. If there's someone out there who wants to know about GUI except the GUI you're doing things wrong ...
-
Responsibilities:
Your constructor shouldn't request focus and set the window to visible himself. That's things that should be done in calling code. You don't expect a
Naming:
Your class is named
Input_Window which violates standard Java naming conventions of PascalCase (without underscores).-
Formatting:
An equal sign should be surrounded by spaces:
myslideA = slider(0,10,3,1,true,true);
//and so on...-
Visibilities:
- Your JSliders should most probably be
private. nobody aside from
your InputWindow should have to know about them. If there's someone out there who wants to know about GUI except the GUI you're doing things wrong ...
slidershould also be private. If somebody else uses it, extract it to a utility-class. Hide the information where ever you can. If you don't things may happen with your stuff when you're not watching.
-
Responsibilities:
Your constructor shouldn't request focus and set the window to visible himself. That's things that should be done in calling code. You don't expect a
JFrame to show up on instantiation either, right?Code Snippets
myslideA = slider(0,10,3,1,true,true);
//and so on...Context
StackExchange Code Review Q#73767, answer score: 4
Revisions (0)
No revisions yet.