#Swing centering pls help
1 messages · Page 1 of 1 (latest)
<@&987246487241105418> please have a look, thanks.
Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.
To center the texts horizontally and have them appear next to each other, you can use a combination of layout managers and alignment properties in JavaFX or Swing. Here's an example for both frameworks:
JavaFX:
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class CenteredTexts extends Application {
@Override
public void start(Stage primaryStage) {
HBox root = new HBox();
root.setAlignment(Pos.CENTER);
Label label1 = new Label("Text 1");
Label label2 = new Label("Text 2");
root.getChildren().addAll(label1, label2);
Scene scene = new Scene(root, 400, 200);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Swing:
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.FlowLayout;
public class CenteredTexts {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER));
JLabel label1 = new JLabel("Text 1");
JLabel label2 = new JLabel("Text 2");
panel.add(label1);
panel.add(label2);
frame.getContentPane().add(panel);
frame.setSize(400, 200);
frame.setVisible(true);
}
}
In both examples, the HBox (JavaFX) or JPanel (Swing) is set to have a centered alignment using Pos.CENTER (JavaFX) or FlowLayout.CENTER (Swing), which centers the labels horizontally. The labels are then added to the container (HBox or JPanel) using their respective add methods.
flowlayout could work if it could exist horizontally
there are tons of other layouts you can try. BoxLayout configured for Y_AXIS, GridBagLayout, etc..
why are you using BorderLayout? BorderLayout.CENTER will use all the space that's available
flowlayout does work horizontally, by default. did you mean vertically?
oh yeah xd i confused it xd
@rustic knot
Your question has been closed due to inactivity.
If it was not resolved yet, feel free to just post a message below
to reopen it, or create a new thread.
Note that usually the reason for nobody calling back is that your
question may have been not well asked and hence no one felt confident
enough answering.
When you reopen the thread, try to use your time to improve the quality
of the question by elaborating, providing details, context, all relevant code
snippets, any errors you are getting, concrete examples and perhaps also some
screenshots. Share your attempt, explain the expected results and compare
them to the current results.
Also try to make the information easily accessible by sharing code
or assignment descriptions directly on Discord, not behind a link or
PDF-file; provide some guidance for long code snippets and ensure
the code is well formatted and has syntax highlighting. Kindly read through
https://stackoverflow.com/help/how-to-ask for more.
With enough info, someone knows the answer for sure 👍
this is most far i got
using this
i just dont know how to center in vertically
(have it in center of the screen)
it is centered, its just the font is smaller. try setAlignmentX