Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ChangeTextColor.java
- package form;
- import java.awt.Color;
- import javax.swing.JFrame;
- public class ChangeTextColor extends javax.swing.JFrame {
- private int currentPosition = 0;
- private final String text = "Programming Is Fun";
- private final int maxLeftOffset = 0;
- private final int maxRightOffset = 200;
- public ChangeTextColor() {
- initComponents();
- updateText();
- setExtendedState(JFrame.MAXIMIZED_HORIZ);
- setVisible(true);
- setResizable(false);
- }
- private void updateText() {
- teks.setText(text);
- teks.setForeground(getSelectedColor());
- int labelWidth = teks.getPreferredSize().width;
- int panelWidth = panelTeks.getWidth();
- if (currentPosition < maxLeftOffset) {
- currentPosition = maxLeftOffset;
- } else if (currentPosition + labelWidth > panelWidth - maxLeftOffset) {
- currentPosition = panelWidth - labelWidth - maxLeftOffset;
- }
- teks.setBounds(currentPosition, 46, labelWidth, 20);
- }
- private Color getSelectedColor() {
- if (red.isSelected()) {
- return Color.RED;
- } else if (yellow.isSelected()) {
- return Color.YELLOW;
- } else if (black.isSelected()) {
- return Color.BLACK;
- } else if (orange.isSelected()) {
- return Color.ORANGE;
- } else if (green.isSelected()) {
- return Color.GREEN;
- } else {
- return Color.BLACK; // Default color
- }
- }
- @SuppressWarnings("unchecked")
- // <editor-fold defaultstate="collapsed" desc="Generated Code">
- private void initComponents() {
- colors = new javax.swing.ButtonGroup();
- jPanel1 = new javax.swing.JPanel();
- panelTeks = new javax.swing.JPanel();
- teks = new javax.swing.JLabel();
- red = new javax.swing.JRadioButton();
- yellow = new javax.swing.JRadioButton();
- black = new javax.swing.JRadioButton();
- orange = new javax.swing.JRadioButton();
- green = new javax.swing.JRadioButton();
- left = new javax.swing.JButton();
- right = new javax.swing.JButton();
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- jPanel1.setBackground(new java.awt.Color(255, 255, 255));
- panelTeks.setBackground(new java.awt.Color(255, 255, 255));
- panelTeks.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
- teks.setFont(new java.awt.Font("Segoe UI", 1, 14)); // NOI18N
- teks.setText("Programming Is Fun");
- javax.swing.GroupLayout panelTeksLayout = new javax.swing.GroupLayout(panelTeks);
- panelTeks.setLayout(panelTeksLayout);
- panelTeksLayout.setHorizontalGroup(
- panelTeksLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(panelTeksLayout.createSequentialGroup()
- .addGap(88, 88, 88)
- .addComponent(teks)
- .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- );
- panelTeksLayout.setVerticalGroup(
- panelTeksLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelTeksLayout.createSequentialGroup()
- .addContainerGap(46, Short.MAX_VALUE)
- .addComponent(teks)
- .addGap(32, 32, 32))
- );
- colors.add(red);
- red.setText("Red");
- red.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- redActionPerformed(evt);
- }
- });
- colors.add(yellow);
- yellow.setText("Yellow");
- yellow.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- yellowActionPerformed(evt);
- }
- });
- colors.add(black);
- black.setText("Black");
- black.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- blackActionPerformed(evt);
- }
- });
- colors.add(orange);
- orange.setText("Orange");
- orange.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- orangeActionPerformed(evt);
- }
- });
- colors.add(green);
- green.setText("Green");
- green.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- greenActionPerformed(evt);
- }
- });
- left.setText("<=");
- left.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- leftActionPerformed(evt);
- }
- });
- right.setText("=>");
- right.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- rightActionPerformed(evt);
- }
- });
- javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
- jPanel1.setLayout(jPanel1Layout);
- jPanel1Layout.setHorizontalGroup(
- jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(jPanel1Layout.createSequentialGroup()
- .addGap(7, 7, 7)
- .addComponent(red)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(yellow)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(black)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(orange)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(green)
- .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- .addComponent(panelTeks, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
- .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(left, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(right, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGap(97, 97, 97))
- );
- jPanel1Layout.setVerticalGroup(
- jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(jPanel1Layout.createSequentialGroup()
- .addContainerGap()
- .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(black, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(red)
- .addComponent(yellow)
- .addComponent(orange)
- .addComponent(green)))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(panelTeks, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(right)
- .addComponent(left))
- .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- );
- javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
- getContentPane().setLayout(layout);
- layout.setHorizontalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addContainerGap()
- .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addContainerGap())
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- );
- pack();
- }// </editor-fold>
- private void yellowActionPerformed(java.awt.event.ActionEvent evt) {
- updateText();
- }
- private void redActionPerformed(java.awt.event.ActionEvent evt) {
- updateText();
- }
- private void blackActionPerformed(java.awt.event.ActionEvent evt) {
- updateText();
- }
- private void orangeActionPerformed(java.awt.event.ActionEvent evt) {
- updateText();
- }
- private void greenActionPerformed(java.awt.event.ActionEvent evt) {
- updateText();
- }
- private void leftActionPerformed(java.awt.event.ActionEvent evt) {
- if (currentPosition > maxLeftOffset) {
- currentPosition -= 10; // Adjust this value for the desired movement
- updateText();
- }
- }
- private void rightActionPerformed(java.awt.event.ActionEvent evt) {
- if (currentPosition < maxRightOffset) {
- currentPosition += 10; // Adjust this value for the desired movement
- updateText();
- }
- }
- public static void main(String args[]) {
- /* Set the Nimbus look and feel */
- //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
- /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
- * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
- */
- try {
- for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
- if ("Nimbus".equals(info.getName())) {
- javax.swing.UIManager.setLookAndFeel(info.getClassName());
- break;
- }
- }
- } catch (ClassNotFoundException ex) {
- java.util.logging.Logger.getLogger(ChangeTextColor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (InstantiationException ex) {
- java.util.logging.Logger.getLogger(ChangeTextColor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (IllegalAccessException ex) {
- java.util.logging.Logger.getLogger(ChangeTextColor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (javax.swing.UnsupportedLookAndFeelException ex) {
- java.util.logging.Logger.getLogger(ChangeTextColor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- }
- //</editor-fold>
- //</editor-fold>
- /* Create and display the form */
- java.awt.EventQueue.invokeLater(new Runnable() {
- public void run() {
- new ChangeTextColor().setVisible(true);
- }
- });
- }
- // Variables declaration - do not modify
- private javax.swing.JRadioButton black;
- private javax.swing.ButtonGroup colors;
- private javax.swing.JRadioButton green;
- private javax.swing.JPanel jPanel1;
- private javax.swing.JButton left;
- private javax.swing.JRadioButton orange;
- private javax.swing.JPanel panelTeks;
- private javax.swing.JRadioButton red;
- private javax.swing.JButton right;
- private javax.swing.JLabel teks;
- private javax.swing.JRadioButton yellow;
- // End of variables declaration
- }
Advertisement
Add Comment
Please, Sign In to add comment